Source of: /manual/en/control-structures.do.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.do.while.php',
1 => 'do-while',
),
'up' =>
array (
0 => 'language.control-structures.php',
1 => 'Control Structures',
),
'prev' =>
array (
0 => 'control-structures.while.php',
1 => 'while',
),
'next' =>
array (
0 => 'control-structures.for.php',
1 => 'for',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="control-structures.do.while" class="sect1">
<h2 class="title"><i>do-while</i></h2>
<p class="simpara">
<i>do-while</i> loops are very similar to
<i>while</i> loops, except the truth expression is
checked at the end of each iteration instead of in the beginning.
The main difference from regular <i>while</i> loops is
that the first iteration of a <i>do-while</i> loop is
guaranteed to run (the truth expression is only checked at the end
of the iteration), whereas it may not necessarily run with a
regular <i>while</i> loop (the truth expression is
checked at the beginning of each iteration, if it evaluates to
<b><tt class="constant">FALSE</tt></b> right from the beginning, the loop
execution would end immediately).
</p>
<p class="para">
There is just one syntax for <i>do-while</i> loops:
</p><div class="informalexample">
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br />$i </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">;<br />do {<br /> echo </span><span style="color: #0000BB">$i</span><span style="color: #007700">;<br />} while (</span><span style="color: #0000BB">$i </span><span style="color: #007700">> </span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
<p class="simpara">
The above loop would run one time exactly, since after the first
iteration, when truth expression is checked, it evaluates to
<b><tt class="constant">FALSE</tt></b> (<var class="varname">$i</var> is not bigger than 0) and the loop
execution ends.
</p>
<p class="para">
Advanced C users may be familiar with a different usage of the
<i>do-while</i> loop, to allow stopping execution in
the middle of code blocks, by encapsulating them with
<i>do-while</i> (0), and using the <a href="control-structures.break.php" class="link"><i>break</i></a>
statement. The following code fragment demonstrates this:
</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: #007700">do {<br /> if (</span><span style="color: #0000BB">$i </span><span style="color: #007700">< </span><span style="color: #0000BB">5</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"i is not big enough"</span><span style="color: #007700">;<br /> break;<br /> }<br /> </span><span style="color: #0000BB">$i </span><span style="color: #007700">*= </span><span style="color: #0000BB">$factor</span><span style="color: #007700">;<br /> if (</span><span style="color: #0000BB">$i </span><span style="color: #007700">< </span><span style="color: #0000BB">$minimum_limit</span><span style="color: #007700">) {<br /> break;<br /> }<br /> echo </span><span style="color: #DD0000">"i is ok"</span><span style="color: #007700">;<br /><br /> </span><span style="color: #FF8000">/* process i */<br /><br /></span><span style="color: #007700">} while (</span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
<p class="simpara">
Don't worry if you don't understand this right away or at all.
You can code scripts and even powerful scripts without using this
'feature'.
Since PHP 5.3.0, it is possible to use
<a href="control-structures.goto.php" class="link"><i>goto</i></a>
operator instead of this hack.
</p>
</div><?php manual_footer(); ?>