truegilly Posted January 24, 2007 Share Posted January 24, 2007 Hi there,im a total newbie with php and was wondering if a php function can return more than one value.the function below will return a variable that contains the name of a colour. in the function i have queried my database and stored values in the variables [color=red]$Target [/color]and [color=red]$Actual[/color].it would be nice to be able to return more values than just the return, and was wondering if this is possible.below is my function[code]function consolidate(){ //perform query $query = 'SELECT Target_No_Of_VC10, Actual_No_Of_VC10 FROM vc10_node'; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); // get a single row from the database $row = mysql_fetch_row($result); $Target = $row[0]; $Actual = $row[1]; $colour = null; // simple function if ($Target == $Actual) { $colour = "green"; return $colour; } else if ($Actual < $Target) { $colour = "red"; return $colour; } else { $Actual > $Target; $colour = "blue"; return $colour; } }[/code]what i would like to do is return the value of the the variables $target and $actual without having to write a new function.any help would be appreciated.truegilly Quote Link to comment https://forums.phpfreaks.com/topic/35522-can-a-php-function-return-more-than-1-value/ Share on other sites More sharing options...
Jessica Posted January 24, 2007 Share Posted January 24, 2007 you can send it back in an array$ret['target'] = $target;$ret['actual'] = $actual;return $ret Quote Link to comment https://forums.phpfreaks.com/topic/35522-can-a-php-function-return-more-than-1-value/#findComment-168139 Share on other sites More sharing options...
onlyican Posted January 24, 2007 Share Posted January 24, 2007 you can, return an array, and use that datafor example[code=php:0]function MyFunction(){$MyName = "Jamie";$MyCountry = "UK";return array($MyName, $MyCountry);}list($MyName, $MyCountry) = MyFunction();echo $MyName."<br />\n".$MyCountry."<br />\n";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/35522-can-a-php-function-return-more-than-1-value/#findComment-168141 Share on other sites More sharing options...
truegilly Posted January 24, 2007 Author Share Posted January 24, 2007 thanks ill give these suggestions a try !truegilly ;) Quote Link to comment https://forums.phpfreaks.com/topic/35522-can-a-php-function-return-more-than-1-value/#findComment-168261 Share on other sites More sharing options...
truegilly Posted January 24, 2007 Author Share Posted January 24, 2007 Hi Guys,thanks for the reply's...had a go and it is returning values but i am getting letters coming back rather than numbers, which is what they are set to in my database.the target is set to 5 ($Target)and the actual is set to 6 ($Actual)any ideas why this is happening ??any help is most appreciated !! ??? Quote Link to comment https://forums.phpfreaks.com/topic/35522-can-a-php-function-return-more-than-1-value/#findComment-168273 Share on other sites More sharing options...
Jessica Posted January 24, 2007 Share Posted January 24, 2007 How are you trying to access them? Quote Link to comment https://forums.phpfreaks.com/topic/35522-can-a-php-function-return-more-than-1-value/#findComment-168289 Share on other sites More sharing options...
redbullmarky Posted January 24, 2007 Share Posted January 24, 2007 now you're returning an array, make sure you're accessing the return result's elements, not the whole thing.[code]<?php$ret = consolidate();// correctecho $ret['actual'];echo $ret['target'];// not correctecho $ret;?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/35522-can-a-php-function-return-more-than-1-value/#findComment-168302 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.