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

Generators Don’t Return

Generators were not allowed to have return values. This feature was added in PHP 7.0, with the getReturn method.

PHP code

<?php

function foo() {
    yield 'a';
    
    return 2;
}

foreach(foo() as $a) {
    print $a.PHP_EOL;
}

?>

Before

PHP Fatal error:  Generators cannot return values using "return" 

Fatal error: Generators cannot return values using "return" 

After

a

PHP version change

This behavior changed in 7.0.

Error Messages

Extension

© 2023-2026, Damien Seguy - Exakat