I am creating a simple web site in PHP, and I want to return multiple variables from one function. Is that possible or not?
And if possible, then how?
I am creating a simple web site in PHP, and I want to return multiple variables from one function. Is that possible or not?
And if possible, then how?
Put them into an array:
function myFunction() {
return array('name' => 'Joe', 'birthday' => '1976. 08. 29');
}
So after this, you can say:
$data = myFunction();
echo $data['name'] . "\n";
echo $data['birthday'];
If your array is not fixed, then you can loop through all of the keys and items:
foreach ($data as $key => $value) {
echo $key . ": " . $value . "\n";
}
You can return an array,
function doSth(){
$a = 2;
$b = 5
$c = 9;
return array($a, $b, $c);
}
and then use the list()
method to get back single values:
list($a, $b, $c) = doSth();
echo $a; echo $b; echo $c;
You can do this using arrays and the list()
function:
function doSomething(){
// do stuff
return [$a, $b];
}
list($a, $b) = doSomething();
< ?php
function multiple_returns()
{
return array( 1, 2, "three" );
}
?>