Return 2 values from Function – PHP



Hi, im just wondering if some how possible to return 2 values from the same function that arn’t conditional, that is it always returns 2 values.

for e.g. (this is not what my function actually does)

function myfunction($input1, $input2){
$output1 = $input1 + 1
$output2 = $input2 + 2
return $output1;
return $output2; // i know this won't work but is there a way to achive this

If that is not possible, how can i use global varibles in php, i don’t seem to able use them for some reason they don’t seem to be recognised within functions even if i define them before defining the functions.

Any Help with this Matter is greatly apprecaited Thanks,


You could do this in two ways.

Firstly, you could put the two values you wish to return together as an array, and return the array as one returned value:

return array($output1,$output2)

but then the script that calls the function needs to then break the array apart to get the individual values.

The more easier way is to use the pass by variable instead of the pass by value in the function argument list. Here an example:

function myfnc($input1,$input2,&$output1,&$output2)

In this case, I return nothing, all the values to be changed are in the arguments. If I call this function in my script as:


then after the call, $out1 will be set to 9 and $out2 will be set to 1.


In the second method, are u saying the in the main program, that you can access the output which are changed by the function???

Thanks for the help


Yes basically. In most software languages, you can pass an argument by value or by reference. The default is usually by value, meaning that the value of the variable gets copied to a memory location when the function is called.

Passing by reference, however, does not copy the value of the argument to another memory location, but rather passes the memory location to the function. So in this way any changes made to the variable within the function is actually made to the original variable externally to the function call.

This is the basic idea. How it is really implemented in PHP is unknown to me.

Before PHP5, you would pass an argument by reference in the call to the argument, using the "&" symbol to tell PHP that it is to be passed by reference and not variable, as such:

function myfnc($in1,$in2,$out1,$out2) {
... some code ...
// call to the function myfnc, where $out1 and $out2 are passed by reference

But now PHP will give you warnings, that the "&" symbol should be moved to the function instead of in the call to the function, as such:

function myfnc($in1,$in2,&$out1,&$out2) {
... some code ...
// call to the function myfnc, where $out1 and $out2 are passed by reference

But the effect is the same. In the above, the external values of the first two arguments will not be changed, even if they are changed within the function, but the values of the last two arguments will be changed if they are changed within the function.


See chapter Pass by reference in the standard PHP documentation. That will explain it all.



Thanks heaps that reakky helps


You are welcome. See you around.