Is it possible to have a function with 2 returns like this:
function test($testvar)
{
// do something
return $var1;
return $var2;
}
If so, how would I be able to get each return separately?
Is it possible to have a function with 2 returns like this:
function test($testvar)
{
// do something
return $var1;
return $var2;
}
If so, how would I be able to get each return separately?
You can get values of two or more variables by setting them by reference
output:
use globals like:
This will echo 1 2
Add all variables in an array and then finally return the
array
.And then
Since Php 7.1 we have proper destructuring for lists. Thereby you can do things like this:
In a function this would look like this:
Destructuring is a very powerful tool. It's capable of destructuring key=>value pairs as well
Take a look at the new feature page: http://php.net/manual/de/migration71.new-features.php
yes and no, you can't return more than one variable / object, but as you suggest, you can put them into an array and return that, there is no limit to the nesting of arrays so you can just package them up that way to return
In PHP 5.5 there is also a new concept:
generators
, where you can yield multiple values from a function: