<!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>while</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="control-structures.alternative-syntax.html">Alternative syntax for control structures</a></div> <div class="next" style="text-align: right; float: right;"><a href="control-structures.do.while.html">do-while</a></div> <div class="up"><a href="language.control-structures.html">Control Structures</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="control-structures.while" class="sect1"> <h2 class="title"><em>while</em></h2> <p class="verinfo">(PHP 4, PHP 5)</p> <p class="para"> <em>while</em> loops are the simplest type of loop in PHP. They behave just like their C counterparts. The basic form of a <em>while</em> statement is: <div class="informalexample"> <div class="example-contents"> <div class="cdata"><pre> while (expr) statement </pre></div> </div> </div> </p> <p class="simpara"> The meaning of a <em>while</em> statement is simple. It tells PHP to execute the nested statement(s) repeatedly, as long as the <em>while</em> expression evaluates to <strong><code>TRUE</code></strong>. The value of the expression is checked each time at the beginning of the loop, so even if this value changes during the execution of the nested statement(s), execution will not stop until the end of the iteration (each time PHP runs the statements in the loop is one iteration). Sometimes, if the <em>while</em> expression evaluates to <strong><code>FALSE</code></strong> from the very beginning, the nested statement(s) won't even be run once. </p> <p class="para"> Like with the <em>if</em> statement, you can group multiple statements within the same <em>while</em> loop by surrounding a group of statements with curly braces, or by using the alternate syntax: <div class="informalexample"> <div class="example-contents"> <div class="cdata"><pre> while (expr): statement ... endwhile; </pre></div> </div> </div> </p> <p class="para"> The following examples are identical, and both print the numbers 1 through 10: <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/* example 1 */<br /><br /></span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />while (</span><span style="color: #0000BB">$i </span><span style="color: #007700"><= </span><span style="color: #0000BB">10</span><span style="color: #007700">) {<br /> echo </span><span style="color: #0000BB">$i</span><span style="color: #007700">++; </span><span style="color: #FF8000">/* the printed value would be<br /> $i before the increment<br /> (post-increment) */<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #FF8000">/* example 2 */<br /><br /></span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />while (</span><span style="color: #0000BB">$i </span><span style="color: #007700"><= </span><span style="color: #0000BB">10</span><span style="color: #007700">):<br /> echo </span><span style="color: #0000BB">$i</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$i</span><span style="color: #007700">++;<br />endwhile;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="control-structures.alternative-syntax.html">Alternative syntax for control structures</a></div> <div class="next" style="text-align: right; float: right;"><a href="control-structures.do.while.html">do-while</a></div> <div class="up"><a href="language.control-structures.html">Control Structures</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>