Don’t Forget To Yield¶
<?php
function foo() {
yield 'foo';
goo();
}
function too() {
yield 'too';
yield from goo();
}
function hoo() {
yield 'hoo';
yield goo();
}
function goo() {
yield 'goo';
}
foreach(foo() as $a) { print $a.PHP_EOL;}
// goo
foreach(too() as $a) { print $a.PHP_EOL;}
// too goo
foreach(hoo() as $a) { print $a.PHP_EOL;}
// hoo Uncaught Error: Object of class Generator
// could not be converted to string
?>
It is possible to delegate a generator to another generator.
One point to keep in mind is that they should not be called raw, as nothing happens.
And don’t forget the from part of the keyword, otherwise, it yields the generator, instead of running it.
See Also¶
No yield, no loop [Try me]
PHP Features¶
Last updated: 14 July 2026