function foo($a)
{
$b = ...;
$c = ...;
return (both b and c);
}
and so I could get $b value to $first and $c value to $second
I know you can return more than 1 variable by return array($b,$c) but then it should be $var[0] and $var[1] and then I need to type $first = $var[0] and $second = $var[1] and so I’m creating more useless variables
So is it possible to do so without array?
Fundamentally, functions only have one return value. You could return a class with member variables
firstandsecond, or an associative array with keys"first"and"second", but you’ll still only be returning a single object.*Alternatively, you could references to
$firstand$secondinto your function:I’m not a big fan of this approach, though, because it’s not immediately clear from the call-site that
$firstand$secondare going to be modified.* Note that if you return an array, you can always use the short-hand
list($first,$second) = foo(42);.