<?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.else.php',
    1 => 'else',
    2 => 'else',
  ),
  'up' => 
  array (
    0 => 'language.control-structures.php',
    1 => 'Control Structures',
  ),
  'prev' => 
  array (
    0 => 'control-structures.if.php',
    1 => 'if',
  ),
  'next' => 
  array (
    0 => 'control-structures.elseif.php',
    1 => 'elseif/else if',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/control-structures/else.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="control-structures.else" class="sect1">
 <h2 class="title">else</h2>
 <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p>
 <p class="para">
  Often you&#039;d want to execute a statement if a certain condition is
  met, and a different statement if the condition is not met.  This
  is what <code class="literal">else</code> is for.  <code class="literal">else</code>
  extends an <code class="literal">if</code> statement to execute a statement
  in case the expression in the <code class="literal">if</code> statement
  evaluates to <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>.  For example, the following
  code would display <span class="computeroutput">a is greater than
  b</span> if <var class="varname">$a</var> is greater than
  <var class="varname">$b</var>, and <span class="computeroutput">a is NOT greater
  than b</span> otherwise:
  <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 greater than b"</span><span style="color: #007700">;<br />} else {<br />  echo </span><span style="color: #DD0000">"a is NOT greater than b"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>

  The <code class="literal">else</code> statement is only executed if the
  <code class="literal">if</code> expression evaluated to
  <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>, and if there were any
  <code class="literal">elseif</code> expressions - only if they evaluated to
  <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> as well (see <a href="control-structures.elseif.php" class="link">elseif</a>).

 </p>
 <blockquote class="note"><p><strong class="note">Note</strong>: 
  <strong>Dangling else</strong><br />
  <p class="para">
   In case of nested <code class="literal">if</code>-<code class="literal">else</code> statements,
   an <code class="literal">else</code> is always associated with the nearest <code class="literal">if</code>.
   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$a </span><span style="color: #007700">= </span><span style="color: #0000BB">false</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= </span><span style="color: #0000BB">true</span><span style="color: #007700">;<br />if (</span><span style="color: #0000BB">$a</span><span style="color: #007700">)<br />    if (</span><span style="color: #0000BB">$b</span><span style="color: #007700">)<br />        echo </span><span style="color: #DD0000">"b"</span><span style="color: #007700">;<br />else<br />    echo </span><span style="color: #DD0000">"c"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
   Despite the indentation (which does not matter for PHP), the <code class="literal">else</code>
   is associated with the <code class="literal">if ($b)</code>, so the example does not produce
   any output. While relying on this behavior is valid, it is recommended to avoid
   it by using curly braces to resolve potential ambiguities.
  </p>
 </p></blockquote>
</div><?php manual_footer($setup); ?>