<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Send a value to the generator</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="generator.rewind.html">Generator::rewind</a></div> <div class="next" style="text-align: right; float: right;"><a href="generator.throw.html">Generator::throw</a></div> <div class="up"><a href="class.generator.html">Generator</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="generator.send" class="refentry"> <div class="refnamediv"> <h1 class="refname">Generator::send</h1> <p class="verinfo">(PHP 5 >= 5.5.0)</p><p class="refpurpose"><span class="refname">Generator::send</span> — <span class="dc-title">Send a value to the generator</span></p> </div> <div class="refsect1 description" id="refsect1-generator.send-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>Generator::send</strong></span> ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$value</code></span> )</div> <p class="para rdfs-comment"> Sends the given value to the generator as the result of the current <a href="language.generators.syntax.html#control-structures.yield" class="link">yield</a> expression and resumes execution of the generator. </p> <p class="para"> If the generator is not at a <a href="language.generators.syntax.html#control-structures.yield" class="link">yield</a> expression when this method is called, it will first be let to advance to the first <a href="language.generators.syntax.html#control-structures.yield" class="link">yield</a> expression before sending the value. As such it is not necessary to "prime" PHP generators with a <span class="methodname"><a href="generator.next.html" class="methodname">Generator::next()</a></span> call (like it is done in Python). </p> </div> <div class="refsect1 parameters" id="refsect1-generator.send-parameters"> <h3 class="title">Parameters</h3> <dl> <dt> <span class="term"><em><code class="parameter">value</code></em></span> <dd> <p class="para"> Value to send into the generator. This value will be the return value of the <a href="language.generators.syntax.html#control-structures.yield" class="link">yield</a> expression the generator is currently at. </p> </dd> </dt> </dl> </div> <div class="refsect1 examples" id="refsect1-generator.send-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-309"> <p><strong>Example #1 Using <span class="methodname"><strong>Generator::send()</strong></span> to inject values</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">printer</span><span style="color: #007700">() {<br /> while (</span><span style="color: #0000BB">true</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$string </span><span style="color: #007700">= </span><span style="color: #0000BB">yield</span><span style="color: #007700">;<br /> echo </span><span style="color: #0000BB">$string</span><span style="color: #007700">;<br /> }<br />}<br /><br /></span><span style="color: #0000BB">$printer </span><span style="color: #007700">= </span><span style="color: #0000BB">printer</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$printer</span><span style="color: #007700">-></span><span style="color: #0000BB">send</span><span style="color: #007700">(</span><span style="color: #DD0000">'Hello world!'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$printer</span><span style="color: #007700">-></span><span style="color: #0000BB">send</span><span style="color: #007700">(</span><span style="color: #DD0000">'Bye world!'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></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> Hello world! Bye world! </pre></div> </div> </div> </p> </div> <div class="refsect1 returnvalues" id="refsect1-generator.send-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the yielded value. </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="generator.rewind.html">Generator::rewind</a></div> <div class="next" style="text-align: right; float: right;"><a href="generator.throw.html">Generator::throw</a></div> <div class="up"><a href="class.generator.html">Generator</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>