- Building RESTful Web Services with PHP 7
- Haafiz Waheed ud din Ahmad
- 115字
- 2021-07-03 00:02:23
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.
推薦閱讀
- TypeScript入門與實戰
- Mastering matplotlib
- C語言程序設計立體化案例教程
- JavaScript從入門到精通(第3版)
- Reactive Programming With Java 9
- 微信公眾平臺開發:從零基礎到ThinkPHP5高性能框架實踐
- Expert Android Programming
- Hands-On Automation Testing with Java for Beginners
- Oracle 18c 必須掌握的新特性:管理與實戰
- Apache Spark 2.x for Java Developers
- Python機器學習算法: 原理、實現與案例
- Solr Cookbook(Third Edition)
- 現代C++編程實戰:132個核心技巧示例(原書第2版)
- 零基礎學C語言第2版
- 創意UI:Photoshop玩轉APP設計