dongsu4345 2013-10-16 06: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-16 06: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.

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

报告相同问题?

悬赏问题

  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路