yield
is used for both sending and receiving values inside the generator. From the Generator RFC: If nothing was sent (e.g. during foreach iteration) null is returned.
So in your code, the generator is resumed twice:
- once for
($y as $val)
--yield
returns null inside the generator - once for
$y->send('foo')
--yield
returns 'foo' inside the generator
When I ran your code I got 10 lines of output, ending with NULL
string(3) "foo"
NULL
...
string(3) "foo"
NULL