Jump to content


Photo

Question about returning values


  • Please log in to reply
4 replies to this topic

#1 Fira

Fira
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 25 September 2006 - 08:56 AM

Is it possible to return multiple values?

return $lifeleft, $epgain, $apgain;


gets the error

Parse error: parse error, unexpected ',' in C:\WEB_ROOT\battle.php on line 139


If not, is it possible to return an array of more multiple values?

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 25 September 2006 - 09:28 AM

return them in an array
return array($lifeleft, $epgain, $apgain);


If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 Fira

Fira
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 25 September 2006 - 09:36 AM

And if I was to call a value within this returned array? Would I just call it as if it was returned normally?

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 25 September 2006 - 09:42 AM

You could
<?php
$result = myfunc_that_returns_array ();
$lifeleft = $result[0];
$epgain = $result[1];
$apgain = $result[2];
?>

or

<?php
list ($lifeleft, $epgain, $apgain) = myfunc_that_returns_array ();
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 25 September 2006 - 09:54 AM

As an alternative to returning an array, you can also pass the vars by reference
<?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
?>


If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users