-
Notifications
You must be signed in to change notification settings - Fork 101
/
else.xml
80 lines (78 loc) · 2.47 KB
/
else.xml
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
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- $Author$ -->
<!-- EN-Revision: 690c3ea7c7d416c55e2c54d90bfd1f7f4d489288 Maintainer: dallas Status: ready -->
<sect1 xml:id="control-structures.else" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>else</title>
<?phpdoc print-version-for="else"?>
<para>
经常需要在满足某个条件时执行一条语句,而在不满足该条件时执行其它语句,这正是
<literal>else</literal> 的功能。<literal>else</literal> 延伸了
<literal>if</literal> 语句,可以在 <literal>if</literal>
语句中的表达式的值为 &false; 时执行语句。例如以下代码在
<varname>$a</varname> 大于 <varname>$b</varname> 时显示
<computeroutput>a is bigger than b</computeroutput>,反之则显示
<computeroutput>a is NOT bigger than b</computeroutput>:
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
if ($a > $b) {
echo "a is greater than b";
} else {
echo "a is NOT greater than b";
}
?>
]]>
</programlisting>
</informalexample>
<literal>else</literal> 语句仅在 <literal>if</literal> 以及
<literal>elseif</literal>(如果有的话)语句中的表达式的值为
&false; 时执行(参见 <link
linkend="control-structures.elseif">elseif</link>)。
</para>
<note>
<title>悬挂的 else</title>
<para>
在多层嵌套 <literal>if</literal>-<literal>else</literal> 语句的情况下,
<literal>else</literal> 总是与最近的 <literal>if</literal> 进行关联。
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$a = false;
$b = true;
if ($a)
if ($b)
echo "b";
else
echo "c";
?>
]]>
</programlisting>
</informalexample>
虽然存在缩进(对 PHP 来说,无关紧要), 但是 <literal>else</literal> 还是与 <literal>if ($b)</literal>
进行关联,所以以上示例不会产生任何输出。虽然可以依赖此特性,但是推荐使用花括号,避免潜在的歧义问题。
</para>
</note>
</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
-->