[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 422
  • Last Modified:

PHP Functions: Returning more than one variable

I know that to return a variable from a function, you can use

return $GLOBALS['varName'];

However, 'return' can only be used once, meaning that I can only return one variable per function.

Is there a method that I can use to return more than one variable from a single function?

Many thanks for your time.
0
spikeyjames
Asked:
spikeyjames
  • 4
  • 3
  • 2
  • +2
3 Solutions
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
2 ways:
* return an array of values
* use return parameters
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
function test_ref(&$parameter1, &$parameter2)
 { $parameter1 = 1; $parameter2 = 2; }

when calling such a procedure, the variables passed will change their values
0
 
spikeyjamesAuthor Commented:
I'd like to use return params, but it only seems to work once? Such as:

function testFunc($var1, $var2){
$var1Var = $var1;
$var2Var = $var2;

return $var1Var;
return $var2Var; //this one seems to get ignored because return has already been 'executed'.
}
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
return will end the procedure execution immediately.

if you absolutley want to "return" the values:

function testFunc($var1, $var2){
$res = array();

$res["var1Var"] = $var1;
$res["var2Var"] = $var2;


return $res;
}


and when calling:

$values = testFunc(1,2);

echo $values["var1Var"];
echo $values["var2Var"];




0
 
soapergemCommented:
Look very closely at angelIII's code, he gave you the correct solution already. Return "parameters" are not accessed using the "return" command--they are parameters (another word for this is "arguments"). Here is a modified version of the function you created, and this one works:

function testFunc(&$var1, &$var2)
{
    $var1 = [some value];
    $var2 = [some value];
    // we do not need a return command at all
}

The crux of all of this is that there are ampersands (&) in front of the two parameters. THIS IS IMPORTANT. You need those ampersands there. Putting ampersands in front of the parameter means the the function has "permission" to change the actual value. I will give two examples to better explain this:
--------------------------------------------------------------------------------
Example 1 (does not work correctly - no ampersands)

<?php
function testFunc($var1, $var2)
{
    $var1 = 'Hello ';
    $var2 = 'spikeyjames.';
}

testFunc($a, $b);

echo $a;    // this would not output anything
echo $b;
?>

--------------------------------------------------------------------------------
Example 2 (works correctly)

<?php
function testFunc(&$var1, &$var2)
{
    $var1 = 'Hello ';
    $var2 = 'spikeyjames.';
}

testFunc($a, $b);

echo $a;    // this would output 'Hello spikeyjames.'
echo $b;
?>

--------------------------------------------------------------------------------
With the ampersands, the function has access to those variables outside its own scope. So it can then change their values freely. Since you can have a limitless number of parameters like that, as long as you remember to put the ampersands in front of each, you can have limitless return values and you can omit the "return" command.

This might better explain it:
function testFunc(&$returnVal1, &$returnVal2, &$returnVal3, [...] )
{
     // do stuff
     // you don't need the return command
}
0
 
Richard QuadlingSenior Software DeverloperCommented:
Input and Output parameters may be a better way to describe them. I think this is how VB describes them.

The most common way to use them is to also return a true/false.

e.g.

<?php

function doSomething($var1, $var2, &$answer)
 {
 $work = $var1 * $var2;
 if ($work > 100)
  {
  $answer = $work;
  $b_result = True;
  }
 else
  {
  $b_result = False;
  }
 return $b_result;
 }

$value = 0;
if (doSomething(1, 1, $value))
 {
 // $value is now at least 100.
 }
else
 {
 // $value is unchanged as the parameters supplied were not big enough.
 }
?>

sort of thing.

If you do you output parameters, make sure you do NOT alter the value accidentally as you will be releasing that value back to the calling code!!!

e.g.

function doSomething($var1, $var2, &$answer)
 {
 $answer = $var1 * $var2;
 return ($answer > 100);
 }

$answer IS altered even though the function returned false. This would NOT be the normal understanding of a function. If it returns false you would expect nothing else to have changed. Of course, this depends upon your usage, but something to be aware of.

Also, make sure you document this for later reading. If an output param can be modified, your code should make sure it is of an appropriate value.
0
 
CyberGhostCommented:
From your question, I can that see you want returned variable to be global.
There's no easier way to achieve this than...

<?php
 function return_several() {
  global $var1, $var2, $var3;
// this gets $var1, $var2 and $var3 from the $GLOBALS predefined variable, so you can work with them
  $var1 = "abc";
  $var2 = "def";
// this is the same as doing return $GLOBALS['var1'] = "abc"; return $GLOBALS['var2'] = "def";
 }
?>

so, previous script will behave like this:

<?php
 $var3 = "v3"; // assign simple value to $var3
 print "$var1 $var2 $var3"; // will only print two spaces and 'v3', as $var1 and $var2 are empty
 return_several();
 print "$var1 $var2 $var3"; // will now print 'abc def v3', as function return_several changed $var1 and $var2
?>

regards,
CyberGhost
0
 
spikeyjamesAuthor Commented:
Wow, so many great answers! Thanks to all of you for taking the time to help, I will definately keep this page saved for future reference.
0
 
spikeyjamesAuthor Commented:
Increasing points so I can divide them properly. (I wish I could afford to give you more, but I have 25 left :/)
0
 
spikeyjamesAuthor Commented:
CyberGhost, just wanted to let you know your answer was also very useful but I ran out of points.
0
 
CyberGhostCommented:
2 spikeyjames: don't worry, I'm not really doing this to gain points, I'm doing this to help.

Glad you have a solution.
Good luck in further developement.
CyberGhost
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now