Question

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;
    }
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top