return'ing 2 values

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
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

return'ing 2 values

Post by malcolmboston »

ok, standard return

Code: Select all

// function definition
function add1 ($value)
{
  $value = ($value + 1);
  return $value;
}

// call and retrieval
$value = 26;
$number = add1 ($value)
what if i wanted to do...

Code: Select all

function add1 ($value)
{
  $value = ($value + 1);
  $multiply = ($value * 5);
  return $value;
  return $multiply;
}

how would i write my code for actually returning it, eg (complete pseudo code).

Code: Select all

$value,$multiply = add1 ($value);
can it be done without seperate function calls?
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

Cheesy, but accomplishes the request:

Code: Select all

function add1 ($value)
{
  $value = ($value + 1);
  $multiply = ($value * 5);
  $returnї1] = $value;
  $returnї2] = $multiply;
  return $return;
}

$return = add1($value);
$value = $returnї1];
$multiply = $returnї2];
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

yup, I think that's the only way to do it...as soon as the function hits return, it's all over and it dies a horrible death.

putting everything you want into an array and returning the array is the only way to go...
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

Burrito wrote:yup, I think that's the only way to do it...as soon as the function hits return, it's all over and it dies a horrible death.

putting everything you want into an array and returning the array is the only way to go...
Actually, I can think of other ways to do it.. although each is more horrible than the first:

Code: Select all

function add1 ($value)
{
  global $value, $multiply;
  $value = ($value + 1);
  $multiply = ($value * 5);
}
 
add1($value);
(Evil for the use of global-scope variables)

There are others, but I suspect the fill-an-array, return-the-array approach is the 'cleanest' architecturally. I'd be interested in hearing better ways.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I would too but from a function alone it seems like common sense to just use an array.

I have never really thought of trying any other way...
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

another way... pass parmameters as references, but that does get uglier
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

Code: Select all

function doSomething ( $val ) {
  $vals = array();
  $vals[] = $val % 5;
  $vals[] = $val % 6;
  return $vals;
}
list( $ret1, $ret2 ) = doSomething( 929 );
echo "Return value #1: $ret1\t\t Return value #2: $ret2";
Post Reply