TEENFRONT Posted July 8, 2010 Share Posted July 8, 2010 Hey all Is it possible to have a function return more than one var? For example function test() { //do something $title = "batman"; return $title; } echo test(); // echos batman But what if I want to use several things from the function like $title $cast $rating etc? In separate areas around my page. Basically, I'm making a movie review page, I want a function that builds the review bits up. Then I can echo the various bits around the page. So basically can I do this? <? function buildReview() { // get review data from mysql Return $title $cast $review $poster } Echo "<title>$title</title> <img src=\"$poster\"> "; // and so on... ?> so instead of just echo the function, can I echo the returned vars in a function? Cheers!! Link to comment https://forums.phpfreaks.com/topic/207164-function-to-return-several-useable-vars/ Share on other sites More sharing options...
kenrbnsn Posted July 8, 2010 Share Posted July 8, 2010 You can return an array that holds the values and use list to get the individual variables: <?php function buildReview() { // get review data from mysql Return array($title, $cast, $review, $poster); } list($title, $case, $review, $poster) = buildReview(); ?> Ken Link to comment https://forums.phpfreaks.com/topic/207164-function-to-return-several-useable-vars/#findComment-1083180 Share on other sites More sharing options...
wildteen88 Posted July 8, 2010 Share Posted July 8, 2010 beaten to it When you're returning multiple values from functions you'll want to use an array. eg function buildReview() { // get review data from mysql Return array($title, $cast, $review, $poster); } list($title, $cast, $review, $poster) = buildReview(); Echo "<title>$title</title> <img src=\"$poster\"> "; // and so on... ?> Link to comment https://forums.phpfreaks.com/topic/207164-function-to-return-several-useable-vars/#findComment-1083181 Share on other sites More sharing options...
TEENFRONT Posted July 8, 2010 Author Share Posted July 8, 2010 Excellent, didn't know it cooed be used like that.. Awesome. Many thanks guys! Link to comment https://forums.phpfreaks.com/topic/207164-function-to-return-several-useable-vars/#findComment-1083184 Share on other sites More sharing options...
AbraCadaver Posted July 8, 2010 Share Posted July 8, 2010 I thought I hit post earlier but evidently not. You can return a more sensible array to use as an array or as individual vars: function buildReview() { // get review data from mysql return compact('title', 'cast', 'review', 'poster'); } $info = buildReview(); echo $info['title']; //or extract(buildReview()); echo $title; Link to comment https://forums.phpfreaks.com/topic/207164-function-to-return-several-useable-vars/#findComment-1083232 Share on other sites More sharing options...
kenrbnsn Posted July 8, 2010 Share Posted July 8, 2010 You learn something new all the time. Ken Link to comment https://forums.phpfreaks.com/topic/207164-function-to-return-several-useable-vars/#findComment-1083264 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.