官术网_书友最值得收藏!

Generator return expression

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 Yield
Second Yield
return Value

So it clearly shows that calling a generator function in foreach will not return the return statement. Instead, it will just return at every yield. To get the return Value, this syntax: $gen->getReturn() can be used.

主站蜘蛛池模板: 循化| 肥东县| 辽宁省| 子洲县| 句容市| 明水县| 杭州市| 稷山县| 乐平市| 固始县| 东乡县| 且末县| 德州市| 汪清县| 元阳县| 临桂县| 桃园县| 商丘市| 道真| 茌平县| 开平市| 长子县| 大连市| 静海县| 盖州市| 建昌县| 惠安县| 榆社县| 蒙自县| 新宁县| 额敏县| 蒲城县| 台江县| 吉木乃县| 崇左市| 盐山县| 罗山县| 东丽区| 屯留县| 玉环县| 蒙阴县|