downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Our source is open

The syntax highlighted source is automatically generated by PHP from the plaintext script. If you're interested in what's behind the several functions we used, you can always take a look at the source of the following files:

Of course, if you want to see the source of this page, we have it available. You can also browse the SVN repository for this website on svn.php.net.

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">&lt;?php<br />$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;<br />do&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">;<br />}&nbsp;while&nbsp;(</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">&gt;&nbsp;</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>
 <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">&lt;?php<br /></span><span style="color: #007700">do&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"i&nbsp;is&nbsp;not&nbsp;big&nbsp;enough"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">*=&nbsp;</span><span style="color: #0000BB">$factor</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">$minimum_limit</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"i&nbsp;is&nbsp;ok"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;process&nbsp;i&nbsp;*/<br /><br /></span><span style="color: #007700">}&nbsp;while&nbsp;(</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>
 <p class="simpara">
  Don&#039;t worry if you don&#039;t understand this right away or at all.
  You can code scripts and even powerful scripts without using this
  &#039;feature&#039;.
  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(); ?>
 
show source | credits | sitemap | contact | advertising | mirror sites