Source of: /manual/en/control-structures.while.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.control-structures.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'control-structures.while.php',
1 => 'while',
),
'up' =>
array (
0 => 'language.control-structures.php',
1 => 'Control Structures',
),
'prev' =>
array (
0 => 'control-structures.alternative-syntax.php',
1 => 'Alternative syntax for control structures',
),
'next' =>
array (
0 => 'control-structures.do.while.php',
1 => 'do-while',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="control-structures.while" class="sect1">
<h2 class="title"><i>while</i></h2>
<p class="para">
<i>while</i> loops are the simplest type of loop in
PHP. They behave just like their C counterparts. The basic form
of a <i>while</i> statement is:
</p><div class="informalexample">
<div class="example-contents programlisting">
<div class="cdata"><pre>
while (expr)
statement
</pre></div>
</div>
</div><p>
</p>
<p class="simpara">
The meaning of a <i>while</i> statement is simple. It
tells PHP to execute the nested statement(s) repeatedly, as long
as the <i>while</i> expression evaluates to
<b><tt class="constant">TRUE</tt></b>. 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
<i>while</i> expression evaluates to
<b><tt class="constant">FALSE</tt></b> from the very beginning, the nested
statement(s) won't even be run once.
</p>
<p class="para">
Like with the <i>if</i> statement, you can group
multiple statements within the same <i>while</i> loop
by surrounding a group of statements with curly braces, or by
using the alternate syntax:
</p><div class="informalexample">
<div class="example-contents programlisting">
<div class="cdata"><pre>
while (expr):
statement
...
endwhile;
</pre></div>
</div>
</div><p>
</p>
<p class="para">
The following examples are identical, and both print the numbers
1 through 10:
</p><div class="informalexample">
<div class="example-contents programlisting">
<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>
</p>
</div><?php manual_footer(); ?>