title: What is the difference between a generator and an array?
body: <p>Today the PHP team released the <a href="http://php.net/archive/2013.php#id2013-06-20-1"><strong>PHP 5.5.0</strong></a> version, which includes support for <strong>generators</strong>. Reading <a href="http://br1.php.net/manual/en/language.generators.syntax.php"><strong>the documentation</strong></a>, I noticed that it does exactly what it could do with an array.</p>
  
  <p><strong>PHP team <em>generator</em> example</strong>:</p>
  
  <pre><code>// Only PHP 5.5
  function gen_one_to_three() {
      for ($i = 1; $i &lt;= 3; $i++) {
          // Note that $i is preserved between yields.
          yield $i;
      }
  }
  
  $generator = gen_one_to_three();
  foreach ($generator as $value) {
      echo "$value\n";
  }
  </code></pre>
  
  <p><strong>Result</strong>:</p>
  
  <pre><code>1
  2
  3
  </code></pre>
  
  <p>But I can do the same thing using arrays. And I can still keep compatible with earlier versions of PHP.</p>
  
  <p><strong>Take a look</strong>:</p>
  
  <pre><code>// Compatible with 4.4.9!
  function gen_one_to_three() {
      $results = array();
      for ($i = 1; $i &lt;= 3; $i++) {
          $results[] = $i;
      }
  
      return $results;
  }
  
  $generator = gen_one_to_three();
  foreach ($generator as $value) {
      echo "$value\n";
  }
  </code></pre>
  
  <p><strong>So the question is</strong>: what is the purpose of the existence of this new feature? I got to play all examples of documentation without using the new feature, replacing it with array.</p>
  
  <p>Can anyone give a good explanation and perhaps an example that is not necessarily impossible with older versions, but using generators can help in development?</p>