1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<sect1 xml:id="control-structures.for" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>for</title>
<?phpdoc print-version-for="for"?>
<para>
<literal>for</literal> loops are the most complex loops in PHP.
They behave like their C counterparts. The syntax of a
<literal>for</literal> loop is:
<informalexample>
<programlisting>
<![CDATA[
for (expr1; expr2; expr3)
statement
]]>
</programlisting>
</informalexample>
</para>
<simpara>
The first expression (<varname>expr1</varname>) is
evaluated (executed) once unconditionally at the beginning of the
loop.
</simpara>
<simpara>
In the beginning of each iteration,
<varname>expr2</varname> is evaluated. If it evaluates to
&true;, the loop continues and the nested
statement(s) are executed. If it evaluates to
&false;, the execution of the loop ends.
</simpara>
<simpara>
At the end of each iteration, <varname>expr3</varname> is
evaluated (executed).
</simpara>
<simpara>
Each of the expressions can be empty or contain multiple
expressions separated by commas. In <varname>expr2</varname>, all
expressions separated by a comma are evaluated but the result is taken
from the last part.
<varname>expr2</varname> being empty means the loop should
be run indefinitely (PHP implicitly considers it as
&true;, like C). This may not be as useless as
you might think, since often you'd want to end the loop using a
conditional <link
linkend="control-structures.break"><literal>break</literal></link>
statement instead of using the <literal>for</literal> truth
expression.
</simpara>
<para>
Consider the following examples. All of them display the numbers
1 through 10:
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
/* example 1 */
for ($i = 1; $i <= 10; $i++) {
echo $i;
}
/* example 2 */
for ($i = 1; ; $i++) {
if ($i > 10) {
break;
}
echo $i;
}
/* example 3 */
$i = 1;
for (; ; ) {
if ($i > 10) {
break;
}
echo $i;
$i++;
}
/* example 4 */
for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);
?>
]]>
</programlisting>
</informalexample>
</para>
<simpara>
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 <literal>for</literal> loops comes in handy in many
occasions.
</simpara>
<para>
PHP also supports the alternate "colon syntax" for
<literal>for</literal> loops.
<informalexample>
<programlisting>
<![CDATA[
for (expr1; expr2; expr3):
statement
...
endfor;
]]>
</programlisting>
</informalexample>
</para>
<simpara>
It's a common thing to many users to iterate through arrays like in the
example below.
</simpara>
<para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
/*
* This is an array with some data we want to modify
* when running through the for loop.
*/
$people = array(
array('name' => 'Kalle', 'salt' => 856412),
array('name' => 'Pierre', 'salt' => 215863)
);
for($i = 0; $i < count($people); ++$i) {
$people[$i]['salt'] = mt_rand(000000, 999999);
}
?>
]]>
</programlisting>
</informalexample>
</para>
<simpara>
The above code can be slow, because the array size is fetched on
every iteration. Since the size never changes, the loop be easily
optimized by using an intermediate variable to store the size instead
of repeatedly calling <function>count</function>:
</simpara>
<para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$people = array(
array('name' => 'Kalle', 'salt' => 856412),
array('name' => 'Pierre', 'salt' => 215863)
);
for($i = 0, $size = count($people); $i < $size; ++$i) {
$people[$i]['salt'] = mt_rand(000000, 999999);
}
?>
]]>
</programlisting>
</informalexample>
</para>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
|