Jump to content

Archived

This topic is now archived and is closed to further replies.

Fira

Question about returning values

Recommended Posts

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?

Share this post


Link to post
Share on other sites
return them in an array
[code]
return array($lifeleft, $epgain, $apgain);

[/code]

Share this post


Link to post
Share on other sites
And if I was to call a value within this returned array? Would I just call it as if it was returned normally?

Share this post


Link to post
Share on other sites
You could
[code]
<?php
$result = myfunc_that_returns_array ();
$lifeleft = $result[0];
$epgain = $result[1];
$apgain = $result[2];
?>[/code]

or

[code]
<?php
list ($lifeleft, $epgain, $apgain) = myfunc_that_returns_array ();
?>
[/code]

Share this post


Link to post
Share on other sites
As an alternative to returning an array, you can also pass the vars by reference
[code]<?php
function myfunc_that_not_return_array (&$x, &$y, &$z) {
        // calculation code here
    $x = 42;
    $y = 0.05;
    $z = -30;
}

$lifeleft = $epgain = $apgain = 0;  // initialise vars

myfunc_that_not_return_array ($lifeleft, $epgain, $apgain);  // call function

printf ('%d, %0.2f, %d', $lifeleft, $epgain, $apgain); // --> 42, 0.05, -30
?>
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.