<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = 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 => 'uk',
  ),
  'this' => 
  array (
    0 => 'control-structures.do.while.php',
    1 => 'do-while',
    2 => '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',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/control-structures/do-while.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="control-structures.do.while" class="sect1">
 <h2 class="title">do-while</h2>
 <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p>
 <p class="simpara">
  <code class="literal">do-while</code> loops are very similar to
  <code class="literal">while</code> loops, except the truth expression is
  checked at the end of each iteration instead of in the beginning.
  The main difference from regular <code class="literal">while</code> loops is
  that the first iteration of a <code class="literal">do-while</code> 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 <code class="literal">while</code> loop (the truth expression is
  checked at the beginning of each iteration, if it evaluates to
  <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> right from the beginning, the loop
  execution would end immediately).
 </p>
 <p class="para">
  There is just one syntax for <code class="literal">do-while</code> loops:

  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">&gt; </span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </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
   <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> (<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
  <code class="literal">do-while</code> loop, to allow stopping execution in
  the middle of code blocks, by encapsulating them with
  <code class="literal">do-while</code> (0), and using the <a href="control-structures.break.php" class="link"><code class="literal">break</code></a>
  statement.  The following code fragment demonstrates this:
  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">do {<br />    if (</span><span style="color: #0000BB">$i </span><span style="color: #007700">&lt; </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">&lt; </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">?&gt;</span></span></code></div>
   </div>

  </div>
 </p>
 <p class="simpara">
  It is possible to use the
  <a href="control-structures.goto.php" class="link"><code class="literal">goto</code></a>
  operator instead of this hack.
 </p>
</div><?php manual_footer($setup); ?>