Sophie

Sophie

distrib > PLD > ac > amd64 > media > dist > by-pkgid > dd8ef74e7a184506d40e4328053fb785 > files > 28

php-manual-ro-20051028-1.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>for</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Manual PHP"
HREF="index.html"><LINK
REL="UP"
TITLE="Control Structures"
HREF="language.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"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=ISO-8859-2"></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"
>Manual PHP</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="control-structures.do.while.html"
ACCESSKEY="P"
>Înapoi</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Cap. 11. Control Structures</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="control-structures.foreach.html"
ACCESSKEY="N"
>Înainte</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="control-structures.for"
><VAR
CLASS="literal"
>for</VAR
></A
></H1
><P
>&#13;    <VAR
CLASS="literal"
>for</VAR
> loops are the most complex loops in PHP.
    They behave like their C counterparts.  The syntax of a
    <VAR
CLASS="literal"
>for</VAR
> loop is:
    <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN2525"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="programlisting"
>for (expr1; expr2; expr3) statement</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
   </P
><P
>&#13;    The first expression (<VAR
CLASS="varname"
>expr1</VAR
>) is
    evaluated (executed) once unconditionally at the beginning of the
    loop.
   </P
><P
>&#13;    In the beginning of each iteration,
    <VAR
CLASS="varname"
>expr2</VAR
> is evaluated.  If it evaluates to
    <TT
CLASS="constant"
><B
>TRUE</B
></TT
>, the loop continues and the nested
    statement(s) are executed.  If it evaluates to
    <TT
CLASS="constant"
><B
>FALSE</B
></TT
>, the execution of the loop ends.
   </P
><P
>&#13;    At the end of each iteration, <VAR
CLASS="varname"
>expr3</VAR
> is
    evaluated (executed).
   </P
><P
>&#13;    Each of the expressions can be empty.
    <VAR
CLASS="varname"
>expr2</VAR
> being empty means the loop should
    be run indefinitely (PHP implicitly considers it as
    <TT
CLASS="constant"
><B
>TRUE</B
></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"
><VAR
CLASS="literal"
>break</VAR
></A
>
    statement instead of using the <VAR
CLASS="literal"
>for</VAR
> truth
    expression.
   </P
><P
>&#13;    Consider the following examples.  All of them display numbers from
    1 to 10:
    <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN2542"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#FF8000">/* example 1 */<br /><br /></font><font color="#007700">for (</font><font color="#0000BB">$i </font><font color="#007700">= </font><font color="#0000BB">1</font><font color="#007700">; </font><font color="#0000BB">$i </font><font color="#007700">&lt;= </font><font color="#0000BB">10</font><font color="#007700">; </font><font color="#0000BB">$i</font><font color="#007700">++) {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#0000BB">$i</font><font color="#007700">;<br />}<br /><br /></font><font color="#FF8000">/* example 2 */<br /><br /></font><font color="#007700">for (</font><font color="#0000BB">$i </font><font color="#007700">= </font><font color="#0000BB">1</font><font color="#007700">; ; </font><font color="#0000BB">$i</font><font color="#007700">++) {<br />&nbsp;&nbsp;&nbsp;&nbsp;if (</font><font color="#0000BB">$i </font><font color="#007700">&gt; </font><font color="#0000BB">10</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#0000BB">$i</font><font color="#007700">;<br />}<br /><br /></font><font color="#FF8000">/* example 3 */<br /><br /></font><font color="#0000BB">$i </font><font color="#007700">= </font><font color="#0000BB">1</font><font color="#007700">;<br />for (; ; ) {<br />&nbsp;&nbsp;&nbsp;&nbsp;if (</font><font color="#0000BB">$i </font><font color="#007700">&gt; </font><font color="#0000BB">10</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#0000BB">$i</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$i</font><font color="#007700">++;<br />}<br /><br /></font><font color="#FF8000">/* example 4 */<br /><br /></font><font color="#007700">for (</font><font color="#0000BB">$i </font><font color="#007700">= </font><font color="#0000BB">1</font><font color="#007700">; </font><font color="#0000BB">$i </font><font color="#007700">&lt;= </font><font color="#0000BB">10</font><font color="#007700">; print </font><font color="#0000BB">$i</font><font color="#007700">, </font><font color="#0000BB">$i</font><font color="#007700">++);<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
>
   </P
><P
>&#13;    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 <VAR
CLASS="literal"
>for</VAR
> loops comes in handy in many
    occasions.
   </P
><P
>&#13;    PHP also supports the alternate "colon syntax" for
    <VAR
CLASS="literal"
>for</VAR
> loops.
    <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN2548"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="programlisting"
>for (expr1; expr2; expr3): statement; ...; endfor;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
   </P
><P
>&#13;    Other languages have a <VAR
CLASS="literal"
>foreach</VAR
> statement to
    traverse an array or hash. PHP 3 has no such construct; PHP 4 does
    (see <A
HREF="control-structures.foreach.html"
>foreach</A
>). In PHP 3, 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
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="control-structures.do.while.html"
ACCESSKEY="P"
>Înapoi</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Acas&#227;</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="control-structures.foreach.html"
ACCESSKEY="N"
>Înainte</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><VAR
CLASS="literal"
>do..while</VAR
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="language.control-structures.html"
ACCESSKEY="U"
>Sus</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><VAR
CLASS="literal"
>foreach</VAR
></TD
></TR
></TABLE
></DIV
></BODY
></HTML
>