dongsu4345 2013-10-15 22:16
浏览 74
已采纳

表达式上下文中“yield”的用法是什么?

As in the php manual yield can use part of a expression. $data = (yield $value); But it doesn’t assigned anything to $myval. Am i doing something wrong here? How we can use it in a expression?

     function test_yield(){
        for ($i=0; $i <10; $i++) { 
             $myval = (yield $i);
             echo " \ ".$myval." / <br />";
        }

    }
    foreach (test_yield() as $value) {
        echo $value;
    }
  • 写回答

1条回答 默认 最新

  • dongwei5794 2013-10-15 22:24
    关注

    From the PHP rfc:generators

    The return value of the yield expression is whatever was sent to the generator using send(). If nothing was sent (e.g. during foreach iteration) null is returned.

    Your foreach code never send()s anything into the generator, so yield always returns null.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部