php-doc-en/language/control-structures/elseif.xml
Christoph Michael Becker 7104ee97ce Removed literal tags from title for readability
Closes GH-250.


git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@351935 c90b9560-bf6c-de11-be94-00142212c4b1
2020-12-06 22:38:36 +00:00

114 lines
3.4 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, you can also
write 'else if' (in two words) and the behavior would be identical
to the one of 'elseif' (in a single word). The syntactic meaning
is slightly different (if you're familiar with C, this is the same
behavior) 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 your
<literal>if</literal>/<literal>elseif</literal> conditions, you must
not separate <literal>else if</literal> into two words, or PHP will
fail with a parse error.
</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
-->