mirror of
https://github.com/php/doc-zh.git
synced 2026-03-24 07:02:15 +01:00
* Fix syntax errors in more examples * Add new PHP ini settings for zend.max_allowed_stack_size, zend.reserved_stack_size, and fiber.stack_size * sync commit id
110 lines
3.5 KiB
XML
110 lines
3.5 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
||
<!-- $Revision$ -->
|
||
<!-- EN-Revision: 22583751fbfdaa3eaa41aeb6470d1343f5cb2c78 Maintainer: dallas Status: ready -->
|
||
<!-- CREDITS: mowangjuanzi, Luffy -->
|
||
<sect1 xml:id="control-structures.elseif" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||
<title>elseif/else if</title>
|
||
<?phpdoc print-version-for="elseif"?>
|
||
<para>
|
||
<literal>elseif</literal>,和此名称暗示的一样,是
|
||
<literal>if</literal> 和 <literal>else</literal> 的组合。和
|
||
<literal>else</literal> 一样,它延伸了 <literal>if</literal>
|
||
语句,可以在原来的 <literal>if</literal> 表达式值为 &false;
|
||
时执行不同语句。但是和 <literal>else</literal> 不一样的是,它仅在
|
||
<literal>elseif</literal> 的条件表达式值为 &true;
|
||
时执行语句。例如以下代码将根据条件分别显示
|
||
<computeroutput>a is bigger than b</computeroutput>,<computeroutput>a
|
||
equal to b</computeroutput> 或者
|
||
<computeroutput>a is smaller than b</computeroutput>:
|
||
<informalexample>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
if ($a > $b) {
|
||
echo "a is bigger than b";
|
||
} elseif ($a == $b) {
|
||
echo "a is equal to b";
|
||
} else {
|
||
echo "a is smaller than b";
|
||
}
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
</informalexample>
|
||
</para>
|
||
<simpara>
|
||
在一个 <literal>if</literal> 语句中可以有多个 <literal>elseif</literal>,其中将会执行第一个表达式值为
|
||
&true;(如果有的话)的 <literal>elseif</literal>。在 PHP 中,也可以写成 <literal>else
|
||
if</literal>(两个单词),它和 <literal>elseif</literal>(一个单词)的行为完全一样。句法分析的含义有少许区别(行为与
|
||
C 相同),但是底线是两者会产生完全一样的行为。
|
||
</simpara>
|
||
<simpara>
|
||
<literal>elseif</literal> 的语句仅在之前的 <literal>if</literal> 和所有之前
|
||
<literal>elseif</literal> 的表达式值为 &false;,并且当前的
|
||
<literal>elseif</literal> 表达式值为 &true; 时执行。
|
||
</simpara>
|
||
<note>
|
||
<simpara>
|
||
必须要注意的是 <literal>elseif</literal> 与 <literal>else if</literal>
|
||
只有在类似上例中使用花括号的情况下才认为是完全相同。如果用冒号来定义
|
||
<literal>if</literal>/<literal>elseif</literal> 条件,必须在一个单词中使用
|
||
<literal>elseif</literal>。如果 <literal>else if</literal> 分割为两个单词,则 PHP 会产生解析错误。
|
||
</simpara>
|
||
</note>
|
||
<para>
|
||
<informalexample>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
|
||
/* 不正确的使用方法: */
|
||
if ($a > $b):
|
||
echo $a." is greater than ".$b;
|
||
else if ($a == $b): // 将无法编译
|
||
echo "The above line causes a parse error.";
|
||
endif;
|
||
]]>
|
||
</programlisting>
|
||
</informalexample>
|
||
|
||
<informalexample>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
/* 正确的使用方法: */
|
||
if ($a > $b):
|
||
echo $a." is greater than ".$b;
|
||
elseif ($a == $b): // 注意使用了一个单词的 elseif
|
||
echo $a." equals ".$b;
|
||
else:
|
||
echo $a." is neither greater than or equal to ".$b;
|
||
endif;
|
||
|
||
?>
|
||
]]>
|
||
</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
|
||
-->
|