1
0
mirror of https://github.com/php/doc-zh.git synced 2026-03-24 07:02:15 +01:00
Files
2021-01-17 21:01:50 +08:00

92 lines
2.7 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.if" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>if</title>
<?phpdoc print-version-for="if"?>
<para>
<literal>if</literal> 结构是很多语言包括 PHP
在内最重要的特性之一它允许按照条件执行代码片段。PHP 的
<literal>if</literal> 结构和 C 语言相似:
<informalexample>
<programlisting>
<![CDATA[
<?php
if (expr)
statement
?>
]]>
</programlisting>
</informalexample>
</para>
<simpara>
如同在<link
linkend="language.expressions">表达式</link>一章中定义的,<replaceable>expr</replaceable>
按照布尔求值。如果 <replaceable>expr</replaceable>
的值为 &true;PHP 将执行 <replaceable>statement</replaceable>,如果值为
&false; ——将忽略 <replaceable>statement</replaceable>。有关哪些值被视为
&false; 的更多信息参见<link
linkend="language.types.boolean.casting">转换为布尔值</link>一节。
</simpara>
<para>
如果 <varname>$a</varname> 大于 <varname>$b</varname>,则以下例子将显示
<computeroutput>a is bigger than b</computeroutput>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
if ($a > $b)
echo "a is bigger than b";
?>
]]>
</programlisting>
</informalexample>
</para>
<para>
经常需要按照条件执行不止一条语句,当然并不需要给每条语句都加上一个
<literal>if</literal> 子句。可以将这些语句放入语句组中。例如,如果
<varname>$a</varname> 大于 <varname>$b</varname>,以下代码将显示
<computeroutput>a is bigger than b</computeroutput> 并且将
<varname>$a</varname> 的值赋给 <varname>$b</varname>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
if ($a > $b) {
echo "a is bigger than b";
$b = $a;
}
?>
]]>
</programlisting>
</informalexample>
</para>
<simpara>
<literal>if</literal> 语句可以无限层地嵌套在其它
<literal>if</literal> 语句中,这给程序的不同部分的条件执行提供了充分的弹性。
</simpara>
</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
-->