Page 1 of 1

multiple returns

Posted: Mon Oct 17, 2005 5:46 am
by UndoUndo
Weirdan | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Hi,

This should be simple for an experience PHP'er. I have a function that I want to provide multpile returns but i'm not too sure how to it i.e

Code: Select all

function myFunction(){
$var1 = 1;
$var2 = 2;
return $var1;
return $var1;

}
$myresults = myFunction();
// do i then refernece myResults as an object?? i.e

$var1 = $myresults.var1
$var2 = $myresults.var2
thanks for any feedback
Weirdan | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Mon Oct 17, 2005 5:59 am
by Ree
return exits your function, so the second return $var1; doesn't get executed. You'd better create multiple functions to return different values, instead of one function trying to return multiple different things. However, if you need to return some values which are very much of the same type, you can return an array.

Posted: Mon Oct 17, 2005 6:06 am
by Weirdan
Ree wrote:However, if you need to return some values which are very much of the same type, you can return an array.
And, using list construct, you can provide simple and convenient way to use the function:

Code: Select all

function twoReturns() {
  $var1 = 1;
  $var2 = 2;
  return array($var1, $var2);
}

list($var1, $var2) = twoReturns();

Posted: Mon Oct 17, 2005 6:06 am
by UndoUndo
Weirdan | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Thanks Ree,


 if 'return' exits the function and i have a 'return' in a 'case' within a 'switch' i dont need to break from the case?

ie

Code: Select all

function myFunc(){

   switch($fruit){
      case 'apple':
      $price = '$50';
      return $price;

      case 'banana' :
      $price = '$40';
      return $price;

      }

}
cheers
Weirdan | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Mon Oct 17, 2005 6:12 am
by Ree
Yeah, you don't. But it looks better this way:

Code: Select all

function myFunc($fruit)
{
  switch($fruit)
  {
    case 'apple':
      $price = 50;
      break;
    case 'banana':
      $price = 40;
      break;
    ...
  }
  return '$' . (string) $price;
}
You're returning $price everywhere, so why not write it once only? It will also help if you later decide to modify the returned price, for example, convert it to another currency (unlikely, but who knows? :D).

Posted: Mon Oct 17, 2005 6:14 am
by UndoUndo
excellent, thanks for yr help Ree, appreciated. apologies for the code probs :D