multiple returns

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
UndoUndo
Forum Newbie
Posts: 6
Joined: Wed Oct 05, 2005 2:31 pm

multiple returns

Post 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]
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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();
UndoUndo
Forum Newbie
Posts: 6
Joined: Wed Oct 05, 2005 2:31 pm

Post 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]
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post 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).
Last edited by Ree on Mon Oct 17, 2005 6:21 am, edited 4 times in total.
UndoUndo
Forum Newbie
Posts: 6
Joined: Wed Oct 05, 2005 2:31 pm

Post by UndoUndo »

excellent, thanks for yr help Ree, appreciated. apologies for the code probs :D
Post Reply