title: How to use PHP generators without foreach?
body: <p>Here's a simple JavaScript generator (via: <a href="http://blog.carbonfive.com/2013/12/01/hanging-up-on-callbacks-generators-in-ecmascript-6/" rel="noreferrer">http://blog.carbonfive.com/2013/12/01/hanging-up-on-callbacks-generators-in-ecmascript-6/</a>)</p>
  
  <pre><code>function* powGenerator() {
    var result = Math.pow(yield "a", yield "b");
    return result;
  }
  
  var g = powGenerator();
  console.log(g.next().value);   // "a", from the first yield
  console.log(g.next(10).value); // "b", from the second
  console.log(g.next(2).value);  // 100, the result
  </code></pre>
  
  <p>I'm trying to model something similar with PHP but it's a bit of a headache.</p>
  
  <pre><code>&lt;?php
  function powGenerator() {
    return pow((yield 'a'), (yield 'b'));
  }
  </code></pre>
  
  <p>Before I go further, I get this error in PHP</p>
  
  <blockquote>
    <p>Fatal error: Generators cannot return values using "return"</p>
  </blockquote>
  
  <p>Ok, so maybe I'll just use another yield to get the final value out? ...</p>
  
  <pre><code>&lt;?php
  function powGenerator() {
    yield pow((yield 'a'), (yield 'b'));
  }
  
  $g = powGenerator(); //=&gt; Generator {#180}
  echo $g-&gt;send(10);   //=&gt; "b"
  echo $g-&gt;send(2);    //=&gt; 100
  </code></pre>
  
  <p>OK, so I got my value back, but there's two major issues here</p>
  
  <ol>
  <li><p><strong>Where did my <code>"a"</code> go?</strong> &mdash; Notice in the JS example I was able to access the both the <code>"a"</code> and the <code>"b"</code> yielded values as well as the <code>100</code> final result.</p></li>
  <li><p><strong>The generator is still not done!</strong> &mdash; I have to call <code>send</code> an additional time to complete the generator</p>
  
  <pre><code>$g-&gt;valid();   //=&gt; true
  $g-&gt;send('?'); //=&gt; null
  $g-&gt;valid();   //=&gt; false
  </code></pre></li>
  </ol>
  
  <hr>
  
  <p>From PHP <a href="http://php.net/manual/en/generator.send.php" rel="noreferrer">Generator::send</a></p>
  
  <blockquote>
    <p><code>public mixed Generator::send ( mixed $value )</code></p>
    
    <p>Sends the given value to the generator as the result of the current <code>yield</code> expression and resumes execution of the generator.</p>
    
    <p>If the generator is not at a <code>yield</code> expression when this method is called, it will first be let to advance to the first <code>yield</code> expression before sending the value. As such it is not necessary to "prime" PHP generators with a <a href="http://php.net/manual/en/generator.next.php" rel="noreferrer">Generator::next()</a> call (like it is done in Python).</p>
  </blockquote>
  
  <p>Emphasis on "As such it is not necessary to "prime" PHP generators with a <code>Generator::next()</code>". OK, but what does that really mean? I don't have to "prime" it like the JavaScript example, but the first yielded value is also getting swallowed.</p>
  
  <p>Can anyone explain how you're meant to step through generators <strong>without</strong> using a <code>foreach</code>?</p>