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

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.

主站蜘蛛池模板: 吉隆县| 拉萨市| 本溪| 阿拉善左旗| 通许县| 仙桃市| 黄山市| 石景山区| 德阳市| 武夷山市| 香河县| 临沧市| 丰城市| 呼图壁县| 新余市| 岳阳县| 谢通门县| 廉江市| 洛浦县| 五莲县| 弥渡县| 玉田县| 遂川县| 通州区| 焦作市| 富顺县| 板桥市| 秦皇岛市| 武夷山市| 伊川县| 博客| 五原县| 沅陵县| 陆河县| 珠海市| 临邑县| 台州市| 潞城市| 元江| 渝中区| 仲巴县|