<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.generators.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'language.generators.overview.php',
    1 => 'Generators overview',
    2 => 'Generators overview',
  ),
  'up' => 
  array (
    0 => 'language.generators.php',
    1 => 'Generators',
  ),
  'prev' => 
  array (
    0 => 'language.generators.php',
    1 => 'Generators',
  ),
  'next' => 
  array (
    0 => 'language.generators.syntax.php',
    1 => 'Generator syntax',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/generators.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.generators.overview" class="sect1">
  <h2 class="title">Generators overview</h2>
  <p class="verinfo">(PHP 5 &gt;= 5.5.0, PHP 7, PHP 8)</p>

  <p class="para">
   Generators provide an easy way to implement simple
   <a href="language.oop5.iterations.php" class="link">iterators</a> without the
   overhead or complexity of implementing a class that implements the
   <span class="classname"><a href="class.iterator.php" class="classname">Iterator</a></span> interface.
  </p>

  <p class="para">
   A generator offers a convenient way to provide data to <a href="control-structures.foreach.php" class="link"><code class="literal">foreach</code></a> loops without
   having to build an array in memory ahead of time, which may cause the program
   to exceed a memory limit or require a considerable amount of
   processing time to generate. Instead, a generator function can be used,
   which is the same as a normal
   <a href="functions.user-defined.php" class="link">function</a>, except that instead
   of
   <a href="functions.returning-values.php" class="link">return</a>ing once, a
   generator can <a href="language.generators.syntax.php#control-structures.yield" class="link"><code class="literal">yield</code></a> as many times as it needs to in order to provide the
   values to be iterated over.
   Like with iterators, random data access is not possible.
  </p>

  <p class="para">
   A simple example of this is to reimplement the <span class="function"><a href="function.range.php" class="function">range()</a></span>
   function as a generator. The standard <span class="function"><a href="function.range.php" class="function">range()</a></span> function
   has to generate an array with every value in it and return it, which can
   result in large arrays: for example, calling
   <strong class="command">range(0, 1000000)</strong> will result in well over 100 MB of
   memory being used.
  </p>

  <p class="para">
   As an alternative, we can implement an <code class="literal">xrange()</code>
   generator, which will only ever need enough memory to create an
   <span class="classname"><a href="class.iterator.php" class="classname">Iterator</a></span> object and track the current state of the
   generator internally, which turns out to be less than 1 kilobyte.
  </p>

  <div class="example" id="example-1">
   <p><strong>Example #1 Implementing <span class="function"><a href="function.range.php" class="function">range()</a></span> as a generator</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">xrange</span><span style="color: #007700">(</span><span style="color: #0000BB">$start</span><span style="color: #007700">, </span><span style="color: #0000BB">$limit</span><span style="color: #007700">, </span><span style="color: #0000BB">$step </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">) {<br />    if (</span><span style="color: #0000BB">$start </span><span style="color: #007700">&lt;= </span><span style="color: #0000BB">$limit</span><span style="color: #007700">) {<br />        if (</span><span style="color: #0000BB">$step </span><span style="color: #007700">&lt;= </span><span style="color: #0000BB">0</span><span style="color: #007700">) {<br />            throw new </span><span style="color: #0000BB">LogicException</span><span style="color: #007700">(</span><span style="color: #DD0000">'Step must be positive'</span><span style="color: #007700">);<br />        }<br /><br />        for (</span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">$start</span><span style="color: #007700">; </span><span style="color: #0000BB">$i </span><span style="color: #007700">&lt;= </span><span style="color: #0000BB">$limit</span><span style="color: #007700">; </span><span style="color: #0000BB">$i </span><span style="color: #007700">+= </span><span style="color: #0000BB">$step</span><span style="color: #007700">) {<br />            yield </span><span style="color: #0000BB">$i</span><span style="color: #007700">;<br />        }<br />    } else {<br />        if (</span><span style="color: #0000BB">$step </span><span style="color: #007700">&gt;= </span><span style="color: #0000BB">0</span><span style="color: #007700">) {<br />            throw new </span><span style="color: #0000BB">LogicException</span><span style="color: #007700">(</span><span style="color: #DD0000">'Step must be negative'</span><span style="color: #007700">);<br />        }<br /><br />        for (</span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">$start</span><span style="color: #007700">; </span><span style="color: #0000BB">$i </span><span style="color: #007700">&gt;= </span><span style="color: #0000BB">$limit</span><span style="color: #007700">; </span><span style="color: #0000BB">$i </span><span style="color: #007700">+= </span><span style="color: #0000BB">$step</span><span style="color: #007700">) {<br />            yield </span><span style="color: #0000BB">$i</span><span style="color: #007700">;<br />        }<br />    }<br />}<br /><br /></span><span style="color: #FF8000">/*<br /> * Note that both range() and xrange() result in the same<br /> * output below.<br /> */<br /><br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">'Single digit odd numbers from range():  '</span><span style="color: #007700">;<br />foreach (</span><span style="color: #0000BB">range</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">9</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">) as </span><span style="color: #0000BB">$number</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$number</span><span style="color: #DD0000"> "</span><span style="color: #007700">;<br />}<br />echo </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #DD0000">'Single digit odd numbers from xrange(): '</span><span style="color: #007700">;<br />foreach (</span><span style="color: #0000BB">xrange</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">9</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">) as </span><span style="color: #0000BB">$number</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$number</span><span style="color: #DD0000"> "</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>The above example will output:</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
Single digit odd numbers from range():  1 3 5 7 9
Single digit odd numbers from xrange(): 1 3 5 7 9
</pre></div>
   </div>
  </div>

  <div class="sect2" id="language.generators.object">
   <h3 class="title"><span class="classname"><a href="class.generator.php" class="classname">Generator</a></span> objects</h3>
   <p class="para">
    When a generator function is called, a new object of the
    internal <span class="classname"><a href="class.generator.php" class="classname">Generator</a></span> class is returned. This object
    implements the <span class="classname"><a href="class.iterator.php" class="classname">Iterator</a></span> interface in much the same
    way as a forward-only iterator object would, and provides methods that can
    be called to manipulate the state of the generator, including sending
    values to and returning values from it.
   </p>
  </div>
 </div><?php manual_footer($setup); ?>