php-doc-en/language/control-structures/for.xml
Nikita Popov 2c3698ad6f Fix CS and typos in for documentation
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@326845 c90b9560-bf6c-de11-be94-00142212c4b1
2012-07-28 09:08:02 +00:00

182 lines
4.3 KiB
XML

<?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><literal>for</literal></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
-->