How to Return Multiple Values From a PHP Function
We primarily use function parameters to get outside data inside a function for further processing. Similarly, we return values from a PHP function to get access to processed data outside the function. You can define functions in PHP with or without a return value.
While a function in PHP can have multiple parameters, it is not possible for it to have multiple return statements. In this tutorial, I'll show you how to return multiple values from a function in PHP.
- Return Statements in PHP
- Using an Array to Return Multiple Values
- Using an Object to Return Multiple Values
Return Statements in PHP
Functions in PHP can have an optional return
statement. When called from inside a function, a return statement immediately halts the execution of any further code. This also includes other return statements. Here is an example:
1 |
<?php
|
2 |
|
3 |
function multiple_returns($a, $b) { |
4 |
$x = 2*$a; |
5 |
$y = 3*$b; |
6 |
|
7 |
return $x; |
8 |
|
9 |
if($y%3 == 0) { |
10 |
echo "Y is: ".$y; |
11 |
}
|
12 |
|
13 |
return $y; |
14 |
}
|
15 |
|
16 |
$m = 0; |
17 |
$n = 0; |
18 |
|
19 |
$m = multiple_returns(5, 18); |
20 |
|
21 |
// list($m, $n) = multiple_returns(5, 18);
|
22 |
|
23 |
// Outputs: Values are: 10 and 0
|
24 |
echo "Values are: ".$m." and ".$n; |
25 |
|
26 |
?>
|
27 |
Note that running the above code does not echo the statement about the value of $y
. This is because the function stops execution after the first return statement. If you uncomment the line where we use list()
to assign variable values, both $m
and $n
will be NULL
because list()
only works with arrays and the function is only returning a number.
Using an Array to Return Multiple Values
We know that a return statement can return any type of value. Therefore, we can also use it to return an array that will contain all the values we actually want to return. We can rewrite the above example as follows to return multiple values:
1 |
<?php
|
2 |
|
3 |
function multiple_returns($a, $b) { |
4 |
$x = 2*$a; |
5 |
$y = 3*$b; |
6 |
|
7 |
return [$x, $y]; |
8 |
}
|
9 |
|
10 |
list($m, $n) = multiple_returns(5, 18); |
11 |
|
12 |
// Outputs: Values are: 10 and 54
|
13 |
echo "Values are: ".$m." and ".$n; |
14 |
|
15 |
?>
|
From the perspective of PHP, you are still returning a single value, but that single value is an array which can contain multiple other values. This is one of the easiest ways in PHP to simulate the return of multiple values by a function.
We are returning only two values in the above example. However, things can get a bit tricky when more values are involved because you have to keep the proper order of the returned values in mind.
Starting from PHP 7.1, you can use list()
with associative arrays. This means that the order in which you return the elements will not affect the assigned values. Here is an example:
1 |
<?php
|
2 |
|
3 |
function multiple_returns($a, $b) { |
4 |
$x = 2*$a; |
5 |
$y = 3*$b; |
6 |
|
7 |
return ['m' => $x, 'n' => $y]; |
8 |
}
|
9 |
|
10 |
list('m' => $m, 'n' => $n) = multiple_returns(5, 18); |
11 |
|
12 |
// Values are: 10 and 54
|
13 |
echo "Values are: ".$m." and ".$n; |
14 |
|
15 |
list('n' => $n, 'm' => $m) = multiple_returns(5, 18); |
16 |
|
17 |
// Values are: 10 and 54
|
18 |
echo "Values are: ".$m." and ".$n; |
19 |
|
20 |
?>
|
You can see that the variables $m
and $n
get the same values in both cases because the values are now being assigned based on keys instead of numerical indices.
You don't even need to use list()
from PHP 7.1 onward because PHP now supports destructuring syntax. We could rewrite the previous example as:
1 |
<?php
|
2 |
|
3 |
function multiple_returns($a, $b) { |
4 |
$x = 2*$a; |
5 |
$y = 3*$b; |
6 |
|
7 |
return ['m' => $x, 'n' => $y]; |
8 |
}
|
9 |
|
10 |
['m' => $m, 'n' => $n] = multiple_returns(5, 18); |
11 |
|
12 |
// Values are: 10 and 54
|
13 |
echo "Values are: ".$m." and ".$n; |
14 |
|
15 |
['n' => $n, 'm' => $m] = multiple_returns(5, 18); |
16 |
|
17 |
// Values are: 10 and 54
|
18 |
echo "Values are: ".$m." and ".$n; |
19 |
|
20 |
?>
|
Using an Object to Return Multiple Values
Another way to return multiple values from a PHP function is to return an object. We can define a class with different properties using public member variables. One disadvantage of this technique is that you will have to write more code, so it will consume more memory to store multiple instances of the class. The advantage is that you will be able to use the same set of variables in multiple places.
1 |
<?php
|
2 |
|
3 |
class ValueStore { |
4 |
public $m; |
5 |
public $n; |
6 |
}
|
7 |
|
8 |
function multiple_returns_class($a, $b) { |
9 |
|
10 |
$my_values = new ValueStore(); |
11 |
$my_values->m = 2*$a; |
12 |
$my_values->n = 3*$b; |
13 |
|
14 |
return $my_values; |
15 |
}
|
16 |
|
17 |
$values = multiple_returns_class(5, 18); |
18 |
|
19 |
// Values are: 10 and 54
|
20 |
echo "Values are: ".$values->m." and ".$values->n; |
21 |
|
22 |
?>
|
As you can see, we were able to successfully get multiple values out of the function by simply creating an object and assigning the values to its various properties.
Final Thoughts
In this tutorial, you learned that PHP doesn't allow you to directly return multiple values from a function. However, you can get around that limitation by packaging multiple values either as an array or an object. After that, you simply need to return the array or object from the function and then access the values later.