Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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

PHP Features

Last updated: 10 September 2026