<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/class.generator.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'generator.rewind.php',
    1 => 'Generator::rewind',
    2 => 'Execute the generator up to and including the first yield',
  ),
  'up' => 
  array (
    0 => 'class.generator.php',
    1 => 'Generator',
  ),
  'prev' => 
  array (
    0 => 'generator.next.php',
    1 => 'Generator::next',
  ),
  'next' => 
  array (
    0 => 'generator.send.php',
    1 => 'Generator::send',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/predefined/generator/rewind.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="generator.rewind" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">Generator::rewind</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.5.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">Generator::rewind</span> &mdash; <span class="dc-title">Execute the generator up to and including the first yield</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-generator.rewind-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>Generator::rewind</strong></span>(): <span class="type"><a href="language.types.void.php" class="type void">void</a></span></div>

  <p class="para rdfs-comment">
   Executes the generator up to and including the <em>first</em> <a href="language.generators.syntax.php#control-structures.yield" class="link"><code class="literal">yield</code></a>.
   If the generator is already at the <em>first</em> <a href="language.generators.syntax.php#control-structures.yield" class="link"><code class="literal">yield</code></a>,
   no action will be taken.
   If the generator has ever advanced beyond a <a href="language.generators.syntax.php#control-structures.yield" class="link"><code class="literal">yield</code></a> expression,
   this method will throw an <span class="classname"><a href="class.exception.php" class="classname">Exception</a></span>.
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    This is the <em>first</em> method called when starting a
    <a href="control-structures.foreach.php" class="link"><code class="literal">foreach</code></a> loop. It will <em>not</em> be
    executed <em>after</em> <a href="control-structures.foreach.php" class="link"><code class="literal">foreach</code></a> loops.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-generator.rewind-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">This function has no parameters.</p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-generator.rewind-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   No value is returned.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-generator.rewind-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 <span class="methodname"><strong>Generator::rewind()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">function </span><span style="color: #0000BB">generator</span><span style="color: #007700">(): </span><span style="color: #0000BB">Generator<br /></span><span style="color: #007700">{<br />    echo </span><span style="color: #DD0000">"I'm a generator!\n"</span><span style="color: #007700">;<br /><br />    for (</span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">; </span><span style="color: #0000BB">$i </span><span style="color: #007700">&lt;= </span><span style="color: #0000BB">3</span><span style="color: #007700">; </span><span style="color: #0000BB">$i</span><span style="color: #007700">++) {<br />        yield </span><span style="color: #0000BB">$i</span><span style="color: #007700">;<br />    }<br />}<br /><br /></span><span style="color: #FF8000">// Initialize the generator<br /></span><span style="color: #0000BB">$generator </span><span style="color: #007700">= </span><span style="color: #0000BB">generator</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// Rewind the generator to the beginning of the first yield expression,<br />// if it's not already there<br /></span><span style="color: #0000BB">$generator</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">rewind</span><span style="color: #007700">(); </span><span style="color: #FF8000">// I'm a generator!<br /><br />// Nothing happens here; the generator is already rewound<br /></span><span style="color: #0000BB">$generator</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">rewind</span><span style="color: #007700">(); </span><span style="color: #FF8000">// No output (NULL)<br /><br />// This rewinds the generator to the first yield expression,<br />// if it's not already there, and iterates over the generator<br /></span><span style="color: #007700">foreach (</span><span style="color: #0000BB">$generator </span><span style="color: #007700">as </span><span style="color: #0000BB">$value</span><span style="color: #007700">) {<br />    </span><span style="color: #FF8000">// After yielding the first value, the generator remains at<br />    // the first yield expression until it resumes execution and advances to the next yield<br />    </span><span style="color: #007700">echo </span><span style="color: #0000BB">$value</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">; </span><span style="color: #FF8000">// 1<br /><br />    </span><span style="color: #007700">break;<br />}<br /><br /></span><span style="color: #FF8000">// Resume and rewind again. No error occurs because the generator has not advanced beyond the first yield<br /></span><span style="color: #0000BB">$generator</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">rewind</span><span style="color: #007700">();<br /><br />echo </span><span style="color: #0000BB">$generator</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">current</span><span style="color: #007700">(), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">; </span><span style="color: #FF8000">// 1<br /><br />// No error occurs, the generator is still at the first yield<br /></span><span style="color: #0000BB">$generator</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">rewind</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// This advances the generator to the second yield expression<br /></span><span style="color: #0000BB">$generator</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">next</span><span style="color: #007700">();<br /><br />try {<br />    </span><span style="color: #FF8000">// This will throw an Exception,<br />    // because the generator has already advanced to the second yield<br />    </span><span style="color: #0000BB">$generator</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">rewind</span><span style="color: #007700">(); </span><span style="color: #FF8000">// Fatal error: Uncaught Exception: Cannot rewind a generator that was already run<br /></span><span style="color: #007700">} catch (</span><span style="color: #0000BB">Exception $e</span><span style="color: #007700">) {<br />    echo </span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">();<br />}<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="examplescode"><pre class="examplescode">I&#039;m a generator!
1
1
Cannot rewind a generator that was already run</pre>
</div>
    </div>
   </div>
  </p>
 </div>


</div><?php manual_footer($setup); ?>