How to return multiple values from a function in PHP. Is it possible to return more than one values when calling a function. This type of question is mostly asked by beginners who just started coding. Do i have to use multiple return statements for returning multiple values.
** Remember function cannot return multiple values but you can return multiple values from a function using an array.
In this article, i’ll explain how to return multiple values from a function using array. Return statement can only return single value at a time. And only one return statement can be used inside a function. So let’s check how to return multiple values.
How to install PHP, MySql, Apache in Ubuntu.
How to Return Multiple Values from a Function in PHP
Inside a function values are returned by using return statement. After return statement function end its execution immediately and pass control back to the line from which it was called.
If you want to return multiple values in PHP then make an array and return it.
NOTE – Only single return statement is used inside a function. You can’t use multiple return statement inside a function to return multiple values.
Shorter Syntax of Array Declaration in PHP 5.4 .
Seems confusing, let’s understand this concept through example.
1. First Example – Return multiple values in the form of array.
1 2 3 4 5 6 7 8 9 10 11 |
/* function pass_multiple_values takes three arguments and return an array. */ function pass_multiple_values($first_arg,$second_arg,$third_arg) { $value1= $first_arg; // Calculated some value and assign it to value1. $value2= $secon_arg; $value3= $third_arg; return array($value1,$value2,$value3); } |
2. Second Example – Returning values in the form of associative array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
/* Simple function return three values in the form of associative array. */ function test() { $a =2; $b =3; $c =4; $arr = array('a'=>$a, 'b'=>$b, 'c'=>$c ); return $arr; } $arg = test(); /* Print associative array. */ print_r($arg); |
Check if value exists in array.
3.
Using compact() Function in PHP to return multiple values
compact() method create array containing variables and their values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
function getValues() { $a = 4; $b = 5; $c = 6; return compact('a', 'b', 'c'); } $val = getValues(); print_r($val); /* Output */ Array ( [a] => 4 [b] => 5 [c] => 6 ) |
How to Retrieve Multiple Values in PHP
We learned how to return multiple values from function in php through arrays. Now next important thing is how to retrieve those values.
1. Using list() method –
list() is a language construct. list() is used to assign a list of variables in one operation. Through list function(), you can assign array items to variables.
Top five most used PHP functions .
1 2 3 4 5 6 7 8 9 10 |
/* list() method */ list($val1, $val2, $val3) = pass_multiple_values($first_arg,$second_arg,$third_arg); /* If you var_dump $val1, $val2 and $val3 you'll find the assigned value is $value1, $value2 and $value3. Let's say if the return value is 2,3, and 4. Then $val1 = 2, $val2=3 and $val3=4. */ |
2. Second approach is to retrieve values using their indexes.
1 2 3 4 5 6 7 |
$get_val = pass_multiple_values($first_arg,$second_arg,$third_arg); echo $get_val[0]; echo $get_val[1]; echo $get_val[2]; |