September 2017
Intermediate to advanced
244 pages
6h 44m
English
As we have seen earlier, on calling a generator function, it returns a value that is being returned by the yield expression. Before PHP7, it didn't have the return keyword returning a value. But since PHP7.0, it is possible to use the return expression as well. Here, I have used an example from the PHP documentation, as it explains it very well:
<?php$gen = (function() { yield "First Yield"; yield "Second Yield"; return "return Value";})();foreach ($gen as $val) { echo $val, PHP_EOL;}echo $gen->getReturn(), PHP_EOL;
It will give the output as:
First YieldSecond Yieldreturn Value
So it clearly shows that calling a generator function in foreach will not return the return statement. Instead, it will just return at ...
Read now
Unlock full access