<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>elseif/else if</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="control-structures.else.html">else</a></div> <div class="next" style="text-align: right; float: right;"><a href="control-structures.alternative-syntax.html">Alternative syntax for control structures</a></div> <div class="up"><a href="language.control-structures.html">Control Structures</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="control-structures.elseif" class="sect1"> <h2 class="title"><em>elseif</em>/<em>else if</em></h2> <p class="verinfo">(PHP 4, PHP 5)</p> <p class="para"> <em>elseif</em>, as its name suggests, is a combination of <em>if</em> and <em>else</em>. Like <em>else</em>, it extends an <em>if</em> statement to execute a different statement in case the original <em>if</em> expression evaluates to <strong><code>FALSE</code></strong>. However, unlike <em>else</em>, it will execute that alternative expression only if the <em>elseif</em> conditional expression evaluates to <strong><code>TRUE</code></strong>. 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>: <div class="informalexample"> <div class="example-contents"> <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 class="simpara"> There may be several <em>elseif</em>s within the same <em>if</em> statement. The first <em>elseif</em> expression (if any) that evaluates to <strong><code>TRUE</code></strong> 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 <em>elseif</em> statement is only executed if the preceding <em>if</em> expression and any preceding <em>elseif</em> expressions evaluated to <strong><code>FALSE</code></strong>, and the current <em>elseif</em> expression evaluated to <strong><code>TRUE</code></strong>. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara"> Note that <em>elseif</em> and <em>else if</em> will only be considered exactly the same when using curly brackets as in the above example. When using a colon to define your <em>if</em>/<em>elseif</em> conditions, you must not separate <em>else if</em> into two words, or PHP will fail with a parse error. </span> </p></blockquote> <p class="para"> <div class="informalexample"> <div class="example-contents"> <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> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="control-structures.else.html">else</a></div> <div class="next" style="text-align: right; float: right;"><a href="control-structures.alternative-syntax.html">Alternative syntax for control structures</a></div> <div class="up"><a href="language.control-structures.html">Control Structures</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>