mirror of
https://github.com/macintoshplus/doc-en.git
synced 2026-04-24 00:58:17 +02:00
bbaf4fea69
Co-authored-by: George Peter Banyard <girgias@php.net>
115 lines
3.5 KiB
XML
115 lines
3.5 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
|
|
<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>, as its name suggests, is a combination
|
|
of <literal>if</literal> and <literal>else</literal>. Like
|
|
<literal>else</literal>, it extends an <literal>if</literal>
|
|
statement to execute a different statement in case the original
|
|
<literal>if</literal> expression evaluates to
|
|
&false;. However, unlike
|
|
<literal>else</literal>, it will execute that alternative
|
|
expression only if the <literal>elseif</literal> conditional
|
|
expression evaluates to &true;. For example, the
|
|
following code would display <computeroutput>a is bigger than
|
|
b</computeroutput>, <computeroutput>a equal to b</computeroutput>
|
|
or <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>
|
|
There may be several <literal>elseif</literal>s within the same
|
|
<literal>if</literal> statement. The first
|
|
<literal>elseif</literal> expression (if any) that evaluates to
|
|
&true; would be executed. In PHP, it's possible to write
|
|
<literal>else if</literal> (in two words) and the behavior would be identical
|
|
to the one of <literal>elseif</literal> (in a single word). The syntactic meaning
|
|
is slightly different (the same behavior as C) but the bottom line
|
|
is that both would result in exactly the same behavior.
|
|
</simpara>
|
|
<simpara>
|
|
The <literal>elseif</literal> statement is only executed if the
|
|
preceding <literal>if</literal> expression and any preceding
|
|
<literal>elseif</literal> expressions evaluated to
|
|
&false;, and the current
|
|
<literal>elseif</literal> expression evaluated to
|
|
&true;.
|
|
</simpara>
|
|
<note>
|
|
<simpara>
|
|
Note that <literal>elseif</literal> and <literal>else if</literal>
|
|
will only be considered exactly the same when using curly brackets
|
|
as in the above example. When using a colon to define
|
|
<literal>if</literal>/<literal>elseif</literal> conditions, the use
|
|
of <literal>elseif</literal> in a single word becomes necessary. PHP
|
|
will fail with a parse error if <literal>else if</literal>
|
|
is split into two words.
|
|
</simpara>
|
|
</note>
|
|
<para>
|
|
<informalexample>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
/* Incorrect Method: */
|
|
if ($a > $b):
|
|
echo $a." is greater than ".$b;
|
|
else if ($a == $b): // Will not compile.
|
|
echo "The above line causes a parse error.";
|
|
endif;
|
|
|
|
|
|
/* Correct Method: */
|
|
if ($a > $b):
|
|
echo $a." is greater than ".$b;
|
|
elseif ($a == $b): // Note the combination of the words.
|
|
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
|
|
-->
|