Yield, Not Return¶
<?php
function foo() {
yield 1;
yield 2;
yield 3;
return ' 4';
}
function goo($a, $b, $c) {
print "$a $b $c";
}
print PHP_EOL;
goo(...foo());
goo(...($g = foo()));
echo $g->getReturn();
?>
This code displays 1 2 3. This is because the ... operator acts as a foreach and runs the generator. It then spreads the values as arguments for the function call, and, in this case, it matches the needed arguments.
Note that named parameters are also supported, with the good PHP version.
The returned value of the generator is lost, in this case. If you need to access the returned values, you must get the generator, and call the getReturn() method.
See Also¶
PHP Features¶
Last updated: 14 July 2026