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? Link to comment https://forums.phpfreaks.com/topic/21943-question-about-returning-values/ 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] Link to comment https://forums.phpfreaks.com/topic/21943-question-about-returning-values/#findComment-98024 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? Link to comment https://forums.phpfreaks.com/topic/21943-question-about-returning-values/#findComment-98029 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] Link to comment https://forums.phpfreaks.com/topic/21943-question-about-returning-values/#findComment-98030 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] Link to comment https://forums.phpfreaks.com/topic/21943-question-about-returning-values/#findComment-98032 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.