Yield, Not Return By Reference
<?php
function &gen() {
yield 'foo';
return 'Literal';
}
$gen = gen();
var_dump($gen->current());
foreach($gen as $a) {
print $a;
}
print $gen->getReturn();
?>
When a method returns a reference, there is a & before its name, in the signature. This means that the function must return a variable, or a property, and not a literal value.
When the same & is added on a generator, this now means that the yielded values are by reference. On the other hand, the returned value mat be a literal value, without generating a warning.
See Also
PHP Features
Last updated: 10 September 2026