<HTML ><HEAD ><TITLE >do..while</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Control Structures" HREF="control-structures.html"><LINK REL="PREVIOUS" TITLE="while" HREF="control-structures.while.html"><LINK REL="NEXT" TITLE="for" HREF="control-structures.for.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="sect1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="control-structures.while.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 12. Control Structures</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="control-structures.for.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="control-structures.do.while" ></A ><TT CLASS="literal" >do..while</TT ></H1 ><P > <TT CLASS="literal" >do..while</TT > loops are very similar to <TT CLASS="literal" >while</TT > loops, except the truth expression is checked at the end of each iteration instead of in the beginning. The main difference from regular <TT CLASS="literal" >while</TT > loops is that the first iteration of a <TT CLASS="literal" >do..while</TT > loop is guaranteed to run (the truth expression is only checked at the end of the iteration), whereas it's may not necessarily run with a regular <TT CLASS="literal" >while</TT > loop (the truth expression is checked at the beginning of each iteration, if it evaluates to <TT CLASS="constant" ><B >FALSE</B ></TT > right from the beginning, the loop execution would end immediately). </P ><P > There is just one syntax for <TT CLASS="literal" >do..while</TT > loops: <DIV CLASS="informalexample" ><A NAME="AEN4906" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$i = 0; do { print $i; } while ($i>0);</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > The above loop would run one time exactly, since after the first iteration, when truth expression is checked, it evaluates to <TT CLASS="constant" ><B >FALSE</B ></TT > ($i is not bigger than 0) and the loop execution ends. </P ><P > Advanced C users may be familiar with a different usage of the <TT CLASS="literal" >do..while</TT > loop, to allow stopping execution in the middle of code blocks, by encapsulating them with <TT CLASS="literal" >do..while</TT >(0), and using the <A HREF="control-structures.break.html" ><TT CLASS="literal" >break</TT ></A > statement. The following code fragment demonstrates this: <DIV CLASS="informalexample" ><A NAME="AEN4915" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >do { if ($i < 5) { print "i is not big enough"; break; } $i *= $factor; if ($i < $minimum_limit) { break; } print "i is ok"; ...process i... } while(0);</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > 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'. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="control-structures.while.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="control-structures.for.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><TT CLASS="literal" >while</TT ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="control-structures.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><TT CLASS="literal" >for</TT ></TD ></TR ></TABLE ></DIV ></BODY ></HTML >