mirror of
https://github.com/php/doc-zh.git
synced 2026-03-24 07:02:15 +01:00
* 翻译 * 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>
89 lines
2.3 KiB
XML
89 lines
2.3 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
||
<!-- $Revision$ -->
|
||
<!-- $Author$ -->
|
||
<!-- EN-Revision: 7104ee97ced1768a3231588dfc0bc0d7eb1117ad Maintainer: dallas Status: ready -->
|
||
|
||
<sect1 xml:id="control-structures.while" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||
<title>while</title>
|
||
<?phpdoc print-version-for="while"?>
|
||
<para>
|
||
<literal>while</literal> 循环是 PHP 中最简单的循环类型。它和
|
||
C 语言中的 <literal>while</literal> 表现地一样。<literal>while</literal>
|
||
语句的基本格式是:
|
||
<informalexample>
|
||
<programlisting>
|
||
<![CDATA[
|
||
while (expr)
|
||
statement
|
||
]]>
|
||
</programlisting>
|
||
</informalexample>
|
||
</para>
|
||
<simpara>
|
||
<literal>while</literal> 语句的含意很简单,它告诉 PHP 只要
|
||
<literal>while</literal> 表达式的值为 &true;
|
||
就重复执行嵌套中的循环语句。表达式的值在每次开始循环时检查,所以即使这个值在循环语句中改变了,语句也不会停止执行,直到本次循环结束。
|
||
如果 <literal>while</literal> 表达式的值一开始就是 &false;,则循环语句一次都不会执行。
|
||
</simpara>
|
||
<para>
|
||
和 <literal>if</literal> 语句一样,可以在 <literal>while</literal>
|
||
循环中用花括号括起一个语句组,或者用替代语法:
|
||
<informalexample>
|
||
<programlisting>
|
||
<![CDATA[
|
||
while (expr):
|
||
statement
|
||
...
|
||
endwhile;
|
||
]]>
|
||
</programlisting>
|
||
</informalexample>
|
||
</para>
|
||
<para>
|
||
下面两个例子完全一样,都显示数字 1 到 10:
|
||
<informalexample>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
/* 示例 1 */
|
||
|
||
$i = 1;
|
||
while ($i <= 10) {
|
||
echo $i++; /* 在自增前(后自增)打印的值将会是 $i */
|
||
}
|
||
|
||
/* 示例 2 */
|
||
|
||
$i = 1;
|
||
while ($i <= 10):
|
||
print $i;
|
||
$i++;
|
||
endwhile;
|
||
?>
|
||
]]>
|
||
</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
|
||
-->
|