Generators Don’t Return¶
In PHP 5.x, 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" in /codes/generatorDontReturn.php on line 6
Fatal error: Generators cannot return values using "return" in /codes/generatorDontReturn.php on line 6
After¶
a
PHP version change¶
This behavior changed in 7.0