title: PHP - Generator, send does not follow the yield order
body: <p>I want to write example codes step by step how to separate tasks in a generator and move them into 2 or more generators in order to achieve cooperative multitasking between them. You can find all of my tests about this <a href="https://github.com/inf3rno/php-tasks/blob/master/test/integration/CooperativeGeneratorTest55.php" rel="nofollow">here</a>.</p>
  
  <p>Generators are logical in some way, but I got stucked by a single step, which I cannot explain why it works this way:</p>
  
  <p>The generator:</p>
  
  <pre><code>    $spy = new Object();
      $spy-&gt;tasks = array();
  
      $createGenerator = function ($i1) use ($spy) {
  
          yield; //(* -&gt; task 1)
          $spy-&gt;tasks[] = $i1;
          yield($i1); //(task 1 -&gt; *)
  
          $i1 = yield; //(* -&gt; task 2)
          //task 2
          $i2 = $i1 + 1;
          $spy-&gt;tasks[] = $i2;
          yield($i2); //(task 2 -&gt; *)
  
          $i2 = yield; //(* -&gt; task 3)
          $i3 = $i2 + 1;
          $spy-&gt;tasks[] = $i3;
          yield($i3); //(task 3 -&gt; *)
  
          $i3 = yield; //(* -&gt; task 4)
          $i4 = $i3 + 1;
          $spy-&gt;tasks[] = $i4;
          yield($i4); //(task 4 -&gt; *)
  
          $i4 = yield; //(* -&gt; task 5)
          $i5 = $i4 + 1;
          $spy-&gt;tasks[] = $i5;
          yield($i5); //(task 5 -&gt; *)
  
      };
  </code></pre>
  
  <p>The test I waited to succeed, but it failed:</p>
  
  <pre><code>    /** @var Generator $generator */
      $generator = $createGenerator(1);
  
      $i1 = $generator-&gt;send(null);
      $generator-&gt;send($i1);
      $i2 = $generator-&gt;send(null);
      $generator-&gt;send($i2);
      $i3 = $generator-&gt;send(null);
      $generator-&gt;send($i3);
      $i4 = $generator-&gt;send(null);
      $generator-&gt;send($i4);
      $i5 = $generator-&gt;send(null);
  
      $this-&gt;assertSame($spy-&gt;tasks, array(1, 2, 3, 4, 5));
      $this-&gt;assertSame(array($i1, $i2, $i3, $i4, $i5), array(1, 2, 3, 4, 5));
  </code></pre>
  
  <p>The test that unexpectedly succeeds:</p>
  
  <pre><code>    /** @var Generator $generator */
      $generator = $createGenerator(1);
  
      $i1 = $generator-&gt;send(null);
      $generator-&gt;send(null); //blank sends needed to skip the yield-yield gaps
      $i2 = $generator-&gt;send($i1);
      $generator-&gt;send(null);
      $i3 = $generator-&gt;send($i2);
      $generator-&gt;send(null);
      $i4 = $generator-&gt;send($i3);
      $generator-&gt;send(null);
      $i5 = $generator-&gt;send($i4);
  
      $this-&gt;assertSame($spy-&gt;tasks, array(1, 2, 3, 4, 5));
      $this-&gt;assertSame(array($i1, $i2, $i3, $i4, $i5), array(1, 2, 3, 4, 5));
  </code></pre>
  
  <p>Can you explain me this odd behavior of generators by double <code>yield</code>?</p>
  
  <p><strong>Conclusion:</strong></p>
  
  <p>The <code>send()</code> always runs the code from the input of a <code>yield</code> to the output of the next <code>yield</code>. So by running a <code>Generator</code> with <code>send()</code>, it always begins with an input, that's why you cannot get the output of the first <code>yield</code> with a <code>send()</code>, and that's why you always got a <code>null</code> return value by the last <code>send()</code>, before the <code>Generator</code> goes to an invalid state... Sadly the PHP manual has a lack of this information...</p>