<HTML ><HEAD ><TITLE >for</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.44"><LINK REL="HOME" TITLE="PHP Manual" HREF="manual.html"><LINK REL="UP" TITLE="Control Structures" HREF="control-structures.html"><LINK REL="PREVIOUS" TITLE="do..while" HREF="control-structures.do.while.html"><LINK REL="NEXT" TITLE="foreach" HREF="control-structures.foreach.html"></HEAD ><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><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.do.while.html" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 11. Control Structures</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="control-structures.foreach.html" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="control-structures.for" ><TT CLASS="literal" >for</TT ></A ></H1 ><P > <TT CLASS="literal" >for</TT > loops are the most complex loops in PHP. They behave like their C counterparts. The syntax of a <TT CLASS="literal" >for</TT > loop is: <DIV CLASS="informalexample" ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="programlisting" > 1 2 for (expr1; expr2; expr3) statement 3 </PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > The first expression (<TT CLASS="replaceable" ><I >expr1</I ></TT >) is evaluated (executed) once unconditionally at the beginning of the loop. </P ><P > In the beginning of each iteration, <TT CLASS="replaceable" ><I >expr2</I ></TT > is evaluated. If it evaluates to <TT CLASS="literal" >TRUE</TT >, the loop continues and the nested statement(s) are executed. If it evaluates to <TT CLASS="literal" >FALSE</TT >, the execution of the loop ends. </P ><P > At the end of each iteration, <TT CLASS="replaceable" ><I >expr3</I ></TT > is evaluated (executed). </P ><P > Each of the expressions can be empty. <TT CLASS="replaceable" ><I >expr2</I ></TT > being empty means the loop should be run indefinitely (PHP implicitly considers it as <TT CLASS="literal" >TRUE</TT >, like C). This may not be as useless as you might think, since often you'd want to end the loop using a conditional <A HREF="control-structures.break.html" ><TT CLASS="literal" >break</TT ></A > statement instead of using the <TT CLASS="literal" >for</TT > truth expression. </P ><P > Consider the following examples. All of them display numbers from 1 to 10: <DIV CLASS="informalexample" ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="programlisting" > 1 2 /* example 1 */ 3 4 for ($i = 1; $i <= 10; $i++) { 5 print $i; 6 } 7 8 /* example 2 */ 9 10 for ($i = 1;;$i++) { 11 if ($i > 10) { 12 break; 13 } 14 print $i; 15 } 16 17 /* example 3 */ 18 19 $i = 1; 20 for (;;) { 21 if ($i > 10) { 22 break; 23 } 24 print $i; 25 $i++; 26 } 27 28 /* example 4 */ 29 30 for ($i = 1; $i <= 10; print $i, $i++) ; 31 </PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > Of course, the first example appears to be the nicest one (or perhaps the fourth), but you may find that being able to use empty expressions in <TT CLASS="literal" >for</TT > loops comes in handy in many occasions. </P ><P > PHP also supports the alternate "colon syntax" for <TT CLASS="literal" >for</TT > loops. <DIV CLASS="informalexample" ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="programlisting" > 1 2 for (expr1; expr2; expr3): statement; ...; endfor; 3 </PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > Other languages have a <TT CLASS="literal" >foreach</TT > statement to traverse an array or hash. PHP3 has no such construct; PHP4 does (see <A HREF="control-structures.foreach.html" >foreach</A >). In PHP3, you can combine <A HREF="control-structures.while.html" >while</A > with the <A HREF="function.list.html" ><B CLASS="function" >list()</B ></A > and <A HREF="function.each.html" ><B CLASS="function" >each()</B ></A > functions to achieve the same effect. See the documentation for these functions for an example. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="control-structures.do.while.html" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="manual.html" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="control-structures.foreach.html" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><TT CLASS="literal" >do..while</TT ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="control-structures.html" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><TT CLASS="literal" >foreach</TT ></TD ></TR ></TABLE ></DIV ></BODY ></HTML >