Is it possible to return multiple items from a function - and assign them to several variables in a single statement - like can be done in some languages (e.g. Python) ?
For example, can I have something like this:
a, b, c,d = foo();
Is it possible to return multiple items from a function - and assign them to several variables in a single statement - like can be done in some languages (e.g. Python) ?
For example, can I have something like this:
a, b, c,d = foo();
A function can not return multiple values, but similar results can be obtained by returning an array.
<?php
function small_numbers()
{
return array (0, 1, 2);
}
list ($zero, $one, $two) = small_numbers();
?>
Tou can do it if you`ll return an array in function and then use list construction:
function foo(){
...
return array('a', 'b', 'c');
}
list($a, $b, $c) = foo();
Yes it is possible. You can return an array from the function and use the using the list
construct to assign the array elements to variables in one statement:
function fun() {
return array(1,2,3);
}
// this will assign 1 to $a, 2 to $b and 3 to $c.
list($a,$b,$c) = fun();