Source of: /manual/en/control-structures.elseif.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.control-structures.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'control-structures.elseif.php',
1 => 'elseif/else if',
),
'up' =>
array (
0 => 'language.control-structures.php',
1 => 'Control Structures',
),
'prev' =>
array (
0 => 'control-structures.else.php',
1 => 'else',
),
'next' =>
array (
0 => 'control-structures.alternative-syntax.php',
1 => 'Alternative syntax for control structures',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="control-structures.elseif" class="sect1">
<h2 class="title"><i>elseif</i>/<i>else if</i></h2>
<p class="para">
<i>elseif</i>, as its name suggests, is a combination
of <i>if</i> and <i>else</i>. Like
<i>else</i>, it extends an <i>if</i>
statement to execute a different statement in case the original
<i>if</i> expression evaluates to
<b><tt class="constant">FALSE</tt></b>. However, unlike
<i>else</i>, it will execute that alternative
expression only if the <i>elseif</i> conditional
expression evaluates to <b><tt class="constant">TRUE</tt></b>. For example, the
following code would display <span class="computeroutput">a is bigger than
b</span>, <span class="computeroutput">a equal to b</span>
or <span class="computeroutput">a is smaller than b</span>:
</p><div class="informalexample">
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">$a </span><span style="color: #007700">> </span><span style="color: #0000BB">$b</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"a is bigger than b"</span><span style="color: #007700">;<br />} elseif (</span><span style="color: #0000BB">$a </span><span style="color: #007700">== </span><span style="color: #0000BB">$b</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"a is equal to b"</span><span style="color: #007700">;<br />} else {<br /> echo </span><span style="color: #DD0000">"a is smaller than b"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
<p class="simpara">
There may be several <i>elseif</i>s within the same
<i>if</i> statement. The first
<i>elseif</i> expression (if any) that evaluates to
<b><tt class="constant">TRUE</tt></b> 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.
</p>
<p class="simpara">
The <i>elseif</i> statement is only executed if the
preceding <i>if</i> expression and any preceding
<i>elseif</i> expressions evaluated to
<b><tt class="constant">FALSE</tt></b>, and the current
<i>elseif</i> expression evaluated to
<b><tt class="constant">TRUE</tt></b>.
</p>
<blockquote><p><b class="note">Note</b>:
<span class="simpara">
Note that <i>elseif</i> and <i>else if</i>
will only be considered exactly the same when using curly brackets
as in the above example. When using a colon to define your
<i>if</i>/<i>elseif</i> conditions, you must
not separate <i>else if</i> into two words, or PHP will
fail with a parse error.
</span>
</p></blockquote>
<p class="para">
</p><div class="informalexample">
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /><br /></span><span style="color: #FF8000">/* Incorrect Method: */<br /></span><span style="color: #007700">if(</span><span style="color: #0000BB">$a </span><span style="color: #007700">> </span><span style="color: #0000BB">$b</span><span style="color: #007700">):<br /> echo </span><span style="color: #0000BB">$a</span><span style="color: #007700">.</span><span style="color: #DD0000">" is greater than "</span><span style="color: #007700">.</span><span style="color: #0000BB">$b</span><span style="color: #007700">;<br />else if(</span><span style="color: #0000BB">$a </span><span style="color: #007700">== </span><span style="color: #0000BB">$b</span><span style="color: #007700">): </span><span style="color: #FF8000">// Will not compile.<br /> </span><span style="color: #007700">echo </span><span style="color: #DD0000">"The above line causes a parse error."</span><span style="color: #007700">;<br />endif;<br /><br /><br /></span><span style="color: #FF8000">/* Correct Method: */<br /></span><span style="color: #007700">if(</span><span style="color: #0000BB">$a </span><span style="color: #007700">> </span><span style="color: #0000BB">$b</span><span style="color: #007700">):<br /> echo </span><span style="color: #0000BB">$a</span><span style="color: #007700">.</span><span style="color: #DD0000">" is greater than "</span><span style="color: #007700">.</span><span style="color: #0000BB">$b</span><span style="color: #007700">;<br />elseif(</span><span style="color: #0000BB">$a </span><span style="color: #007700">== </span><span style="color: #0000BB">$b</span><span style="color: #007700">): </span><span style="color: #FF8000">// Note the combination of the words.<br /> </span><span style="color: #007700">echo </span><span style="color: #0000BB">$a</span><span style="color: #007700">.</span><span style="color: #DD0000">" equals "</span><span style="color: #007700">.</span><span style="color: #0000BB">$b</span><span style="color: #007700">;<br />else:<br /> echo </span><span style="color: #0000BB">$a</span><span style="color: #007700">.</span><span style="color: #DD0000">" is neither greater than or equal to "</span><span style="color: #007700">.</span><span style="color: #0000BB">$b</span><span style="color: #007700">;<br />endif;<br /><br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
</div><?php manual_footer(); ?>