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

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: 10 September 2026