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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
<?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
-->
|