<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = 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',
    2 => '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',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/control-structures/elseif.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="control-structures.elseif" class="sect1">
 <h2 class="title">elseif/else if</h2>
 <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p>
 <p class="para">
  <code class="literal">elseif</code>, as its name suggests, is a combination
  of <code class="literal">if</code> and <code class="literal">else</code>.  Like
  <code class="literal">else</code>, it extends an <code class="literal">if</code>
  statement to execute a different statement in case the original
  <code class="literal">if</code> expression evaluates to
  <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>.  However, unlike
  <code class="literal">else</code>, it will execute that alternative
  expression only if the <code class="literal">elseif</code> conditional
  expression evaluates to <strong><code><a href="reserved.constants.php#constant.true">true</a></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 (</span><span style="color: #0000BB">$a </span><span style="color: #007700">&gt; </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">?&gt;</span></span></code></div>
   </div>

  </div>
 </p>
 <p class="simpara">
  There may be several <code class="literal">elseif</code>s within the same
  <code class="literal">if</code> statement.  The first
  <code class="literal">elseif</code> expression (if any) that evaluates to
  <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> would be executed.  In PHP, it&#039;s possible to write
  <code class="literal">else if</code> (in two words) and the behavior would be identical
  to the one of <code class="literal">elseif</code> (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.
 </p>
 <p class="simpara">
  The <code class="literal">elseif</code> statement is only executed if the
  preceding <code class="literal">if</code> expression and any preceding
  <code class="literal">elseif</code> expressions evaluated to
  <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>, and the current
  <code class="literal">elseif</code> expression evaluated to
  <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>.
 </p>
 <blockquote class="note"><p><strong class="note">Note</strong>: 
  <span class="simpara">
   Note that <code class="literal">elseif</code> and <code class="literal">else if</code>
   will only be considered exactly the same when using curly brackets
   as in the above example.  When using a colon to define
   <code class="literal">if</code>/<code class="literal">elseif</code> conditions, the use
   of <code class="literal">elseif</code> in a single word becomes necessary. PHP
   will fail with a parse error if <code class="literal">else if</code>
   is split into two words.
  </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">/* Incorrect Method: */<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">$a </span><span style="color: #007700">&gt; </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;</span></span></code></div>
   </div>

  </div>

  <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: #FF8000">/* Correct Method: */<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">$a </span><span style="color: #007700">&gt; </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">?&gt;</span></span></code></div>
   </div>

  </div>
 </p>
</div><?php manual_footer($setup); ?>