downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Our source is open

The syntax highlighted source is automatically generated by PHP from the plaintext script. If you're interested in what's behind the several functions we used, you can always take a look at the source of the following files:

Of course, if you want to see the source of this page, we have it available. You can also browse the SVN repository for this website on svn.php.net.

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">&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>
 <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 &#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 <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">&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>
 </p>
</div><?php manual_footer(); ?>
 
show source | credits | sitemap | contact | advertising | mirror sites