1
0
mirror of https://github.com/php/doc-zh.git synced 2026-03-24 07:02:15 +01:00
Files
魔王卷子 31ff89ae15 Update language/control-structures (#112)
* 翻译

* Update break.xml

* Update do-while.xml

补充原文中 "instead of this hack" 的含义。

* Update include.xml

* Update include-once.xml

Co-authored-by: DAI JIE <daijie@php.net>
2021-11-07 18:04:41 +08:00

165 lines
4.2 KiB
XML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- $Author$ -->
<!-- EN-Revision: 7104ee97ced1768a3231588dfc0bc0d7eb1117ad Maintainer: dallas Status: ready -->
<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> 循环是 PHP 中最复杂的循环结构。它的行为和
C 语言的相似。 <literal>for</literal> 循环的语法是:
<informalexample>
<programlisting>
<![CDATA[
for (expr1; expr2; expr3)
statement
]]>
</programlisting>
</informalexample>
</para>
<simpara>
第一个表达式(<varname>expr1</varname>)在循环开始前无条件求值(并执行)一次。
</simpara>
<simpara>
<varname>expr2</varname> 在每次循环开始前求值。如果值为
&true;,则继续循环,执行嵌套的循环语句。如果值为 &false;,则终止循环。
</simpara>
<simpara>
<varname>expr3</varname> 在每次循环之后被求值(并执行)。
</simpara>
<simpara>
每个表达式都可以为空或包括逗号分隔的多个表达式。表达式 <varname>expr2</varname>
中,所有用逗号分隔的表达式都会计算,但只取最后一个结果。<varname>expr2</varname>
为空意味着将无限循环下去(和 C 一样PHP 暗中认为其值为
&true;)。这可能不像想象中那样没有用,因为经常会希望用有条件的 <link
linkend="control-structures.break"><literal>break</literal></link>
语句来结束循环而不是用 <literal>for</literal> 的表达式真值判断。
</simpara>
<para>
考虑以下的例子,它们都显示数字 1 到 10
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
/* 示例 1 */
for ($i = 1; $i <= 10; $i++) {
echo $i;
}
/* 示例 2 */
for ($i = 1; ; $i++) {
if ($i > 10) {
break;
}
echo $i;
}
/* 示例 3 */
$i = 1;
for (;;) {
if ($i > 10) {
break;
}
echo $i;
$i++;
}
/* 示例 4 */
for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);
?>
]]>
</programlisting>
</informalexample>
</para>
<simpara>
当然,第一个例子看上去最简洁(或者有人认为是第四个),但用户可能会发现在
<literal>for</literal> 循环中用空的表达式在很多场合下会很方便。
</simpara>
<para>
PHP 也支持用冒号的 <literal>for</literal> 循环的替代语法。
<informalexample>
<programlisting>
<![CDATA[
for (expr1; expr2; expr3):
statement;
...
endfor;
]]>
</programlisting>
</informalexample>
</para>
<simpara>
有时经常需要像下面这样例子一样对数组进行遍历:
</simpara>
<para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
/*
* 此数组将在遍历的过程中改变其中某些单元的值
*/
$people = Array(
Array('name' => 'Kalle', 'salt' => 856412),
Array('name' => 'Pierre', 'salt' => 215863)
);
for($i = 0; $i < count($people); ++$i)
{
$people[$i]['salt'] = rand(000000, 999999);
}
?>
]]>
</programlisting>
</informalexample>
</para>
<simpara>
以上代码可能执行很慢,因为每次循环时都要计算一遍数组的长度。由于数组的长度始终不变,可以用一个中间变量来储存数组长度以优化而不是不停调用 <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'] = 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
-->