- 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.
推薦閱讀
- 微服務設計(第2版)
- Django+Vue.js商城項目實戰
- Fundamentals of Linux
- The DevOps 2.4 Toolkit
- Julia 1.0 Programming Complete Reference Guide
- 零代碼實戰:企業級應用搭建與案例詳解
- Learning Nessus for Penetration Testing
- UX Design for Mobile
- Continuous Delivery and DevOps:A Quickstart Guide Second Edition
- JavaScript編程精解(原書第2版)
- Node.js 6.x Blueprints
- Spark技術內幕:深入解析Spark內核架構設計與實現原理
- Kotlin語言實例精解
- JavaScript高級程序設計(第4版)
- Analytics for the Internet of Things(IoT)