dongsu4345
dongsu4345
2013-10-16 06:16
浏览 69
已采纳

表达式上下文中“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;
    }

图片转代码服务由CSDN问答提供 功能建议

在php手册中,yield可以使用表达式的一部分。 $ data =(yield $ value); 但它没有为$ myval分配任何内容。 我在这里做错了吗? 我们如何在表达式中使用它?

  function test_yield(){
 for($ i = 0; $ i&lt; 10; $ i ++){
 $  myval =(yield $ i); 
 echo“\”。$ myval。“/&lt; br /&gt;”; 
} 
 
} 
 \ foreach(test_yield()as $ value){
  echo $ value; 
} 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongwei5794
    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.

    点赞 评论

相关推荐