We mainly use function parameters to get external data within a function for further processing. Similarly, we return values from a PHP function to gain access to the data processed outside the function. You can define functions in PHP with or without a return value.
Although a function in PHP can have multiple parameters, it is not possible for it to have multiple return statements. In this tutorial, we will show you how to return multiple values from a function in PHP.
Return declarations in PHP
Functions in PHP may have an option return
declaration. When called from within a function, a return statement immediately stops execution of any other code. This also includes other return statements. Here is an example:
<?php function multiple_returns($a, $b) { $x = 2*$a; $y = 3*$b; return $x; if($y%3 == 0) { echo "Y is: ".$y; } return $y; } $m = 0; $n = 0; $m = multiple_returns(5, 18); // list($m, $n) = multiple_returns(5, 18); // Outputs: Values are: 10 and 0 echo "Values are: ".$m." and ".$n; ?>
Two things you’ll notice here are that executing the above code doesn’t echo the declaration 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
Sara NULL
because list()
it only works with arrays and the function only returns 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 which will contain all the values we actually want to return. We can rewrite the above example as follows to return multiple values:
<?php function multiple_returns($a, $b) { $x = 2*$a; $y = 3*$b; return [$x, $y]; } list($m, $n) = multiple_returns(5, 18); // Outputs: Values are: 10 and 54 echo "Values are: ".$m." and ".$n; ?>
From PHP’s point of view you are still returning a single value, but that single value is an array that can hold multiple other values. This is one of the simplest ways in PHP to simulate returning multiple values from a function.
We’re only returning two values in the example above. However, things can get a little tricky when more values are involved because you need to keep in mind the correct order of the returned values.
As of PHP 7.1, you can use list()
with associative arrays. This means that the order in which you return items will not affect the assigned values. Here is an example:
<?php function multiple_returns($a, $b) { $x = 2*$a; $y = 3*$b; return ['m' => $x, 'n' => $y]; } list('m' => $m, 'n' => $n) = multiple_returns(5, 18); // Values are: 10 and 54 echo "Values are: ".$m." and ".$n; list('n' => $n, 'm' => $m) = multiple_returns(5, 18); // Values are: 10 and 54 echo "Values are: ".$m." and ".$n; ?>
You can see that the variables $m
And $n
get the same values in both cases because the values are now assigned based on keys rather than numeric indexes.
It is not even necessary to use it list()
PHP 7.1 onwards because PHP now supports destructuring syntax. We could rewrite the previous example as:
<?php function multiple_returns($a, $b) { $x = 2*$a; $y = 3*$b; return ['m' => $x, 'n' => $y]; } ['m' => $m, 'n' => $n] = multiple_returns(5, 18); // Values are: 10 and 54 echo "Values are: ".$m." and ".$n; ['n' => $n, 'm' => $m] = multiple_returns(5, 18); // Values are: 10 and 54 echo "Values are: ".$m." and ".$n; ?>
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. A downside to this technique is that you will have to write more code and it will consume more memory to store more instances of the class. The benefit is that you will be able to use the same set of variables in multiple places.
<?php class ValueStore { public $m; public $n; } function multiple_returns_class($a, $b) { $my_values = new ValueStore(); $my_values->m = 2*$a; $my_values->n = 3*$b; return $my_values; } $values = multiple_returns_class(5, 18); // Values are: 10 and 54 echo "Values are: ".$values->m." and ".$values->n; ?>
As you can see, we were able to successfully get multiple values from the function simply by creating an object and assigning the values to its various properties.
Final thoughts
In this tutorial, we learned that PHP doesn’t allow you to directly return multiple values from a function. However, you can get around this limitation by packing multiple values as an array or object. After that, you simply need to return the array or object from the function and then access the values later.