Fira Posted September 25, 2006 Share Posted September 25, 2006 Is it possible to return multiple values?[quote]return $lifeleft, $epgain, $apgain;[/quote]gets the error [quote]Parse error: parse error, unexpected ',' in C:\WEB_ROOT\battle.php on line 139[/quote]If not, is it possible to return an array of more multiple values? Quote Link to comment Share on other sites More sharing options...
Barand Posted September 25, 2006 Share Posted September 25, 2006 return them in an array[code]return array($lifeleft, $epgain, $apgain);[/code] Quote Link to comment Share on other sites More sharing options...
Fira Posted September 25, 2006 Author Share Posted September 25, 2006 And if I was to call a value within this returned array? Would I just call it as if it was returned normally? Quote Link to comment Share on other sites More sharing options...
Barand Posted September 25, 2006 Share Posted September 25, 2006 You could[code]<?php$result = myfunc_that_returns_array ();$lifeleft = $result[0];$epgain = $result[1];$apgain = $result[2];?>[/code]or[code]<?phplist ($lifeleft, $epgain, $apgain) = myfunc_that_returns_array ();?>[/code] Quote Link to comment Share on other sites More sharing options...
Barand Posted September 25, 2006 Share Posted September 25, 2006 As an alternative to returning an array, you can also pass the vars by reference[code]<?phpfunction myfunc_that_not_return_array (&$x, &$y, &$z) { // calculation code here $x = 42; $y = 0.05; $z = -30;}$lifeleft = $epgain = $apgain = 0; // initialise varsmyfunc_that_not_return_array ($lifeleft, $epgain, $apgain); // call functionprintf ('%d, %0.2f, %d', $lifeleft, $epgain, $apgain); // --> 42, 0.05, -30?>[/code] Quote Link to comment 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.