Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 1235

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!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">&lt;?php<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">&gt;&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"a&nbsp;is&nbsp;bigger&nbsp;than&nbsp;b"</span><span style="color: #007700">;<br />}&nbsp;elseif&nbsp;(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"a&nbsp;is&nbsp;equal&nbsp;to&nbsp;b"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"a&nbsp;is&nbsp;smaller&nbsp;than&nbsp;b"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</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 &#039;else if&#039; (in two words) and the behavior would be identical
  to the one of &#039;elseif&#039; (in a single word).  The syntactic meaning
  is slightly different (if you&#039;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">&lt;?php<br /><br /></span><span style="color: #FF8000">/*&nbsp;Incorrect&nbsp;Method:&nbsp;*/<br /></span><span style="color: #007700">if(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">&gt;&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">):<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">.</span><span style="color: #DD0000">"&nbsp;is&nbsp;greater&nbsp;than&nbsp;"</span><span style="color: #007700">.</span><span style="color: #0000BB">$b</span><span style="color: #007700">;<br />else&nbsp;if(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">):&nbsp;</span><span style="color: #FF8000">//&nbsp;Will&nbsp;not&nbsp;compile.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"The&nbsp;above&nbsp;line&nbsp;causes&nbsp;a&nbsp;parse&nbsp;error."</span><span style="color: #007700">;<br />endif;<br /><br /><br /></span><span style="color: #FF8000">/*&nbsp;Correct&nbsp;Method:&nbsp;*/<br /></span><span style="color: #007700">if(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">&gt;&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">):<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">.</span><span style="color: #DD0000">"&nbsp;is&nbsp;greater&nbsp;than&nbsp;"</span><span style="color: #007700">.</span><span style="color: #0000BB">$b</span><span style="color: #007700">;<br />elseif(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">):&nbsp;</span><span style="color: #FF8000">//&nbsp;Note&nbsp;the&nbsp;combination&nbsp;of&nbsp;the&nbsp;words.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">.</span><span style="color: #DD0000">"&nbsp;equals&nbsp;"</span><span style="color: #007700">.</span><span style="color: #0000BB">$b</span><span style="color: #007700">;<br />else:<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">.</span><span style="color: #DD0000">"&nbsp;is&nbsp;neither&nbsp;greater&nbsp;than&nbsp;or&nbsp;equal&nbsp;to&nbsp;"</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">?&gt;</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>