<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.types.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'language.types.boolean.php',
    1 => 'Booleans',
    2 => 'Booleans',
  ),
  'up' => 
  array (
    0 => 'language.types.php',
    1 => 'Types',
  ),
  'prev' => 
  array (
    0 => 'language.types.null.php',
    1 => 'NULL',
  ),
  'next' => 
  array (
    0 => 'language.types.integer.php',
    1 => 'Integers',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/types/boolean.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.types.boolean" class="sect1">
 <h2 class="title">Booleans</h2>

 <p class="simpara">
  The <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> type only has two values, and is used to express
  a truth value. It can be either <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> or <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>.
 </p>

 <div class="sect2" id="language.types.boolean.syntax">
  <h3 class="title">Syntax</h3>
  <p class="para">
   To specify a <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> literal, use the constants <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> or
   <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>. Both are case-insensitive.
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$foo </span><span style="color: #007700">= </span><span style="color: #0000BB">True</span><span style="color: #007700">; </span><span style="color: #FF8000">// assign the value TRUE to $foo<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>

  <p class="para">
   Typically, the result of an <a href="language.operators.php" class="link">operator</a>
   which returns a <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> value is passed on to a
   <a href="language.control-structures.php" class="link">control structure</a>.
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$action </span><span style="color: #007700">= </span><span style="color: #DD0000">"show_version"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$show_separators </span><span style="color: #007700">= </span><span style="color: #0000BB">true</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// == is an operator which tests<br />// equality and returns a boolean<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">$action </span><span style="color: #007700">== </span><span style="color: #DD0000">"show_version"</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">"The version is 1.23"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// this is not necessary...<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">$show_separators </span><span style="color: #007700">== </span><span style="color: #0000BB">TRUE</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">"&lt;hr&gt;\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// ...because this can be used with exactly the same meaning:<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">$show_separators</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">"&lt;hr&gt;\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </div>

 <div class="sect2" id="language.types.boolean.casting">
  <h3 class="title">Converting to boolean</h3>

  <p class="simpara">
   To explicitly convert a value to <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span>, use the
   <code class="literal">(bool)</code> cast. Generally this is not necessary because when
   a value is used in a logical context it will be automatically interpreted
   as a value of type <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span>. For more information see the
   <a href="language.types.type-juggling.php" class="link">Type Juggling</a> page.
  </p>

  <p class="para">
   When converting to <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span>, the following values are considered
   <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>:
  </p>

  <ul class="itemizedlist">
   <li class="listitem">
    <span class="simpara">
     the <a href="language.types.boolean.php" class="link">boolean</a> <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> itself
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     the <a href="language.types.integer.php" class="link">integer</a>
     <code class="literal">0</code> (zero)
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     the <a href="language.types.float.php" class="link">float</a>s
     <code class="literal">0.0</code> and <code class="literal">-0.0</code> (zero)
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     the empty <a href="language.types.string.php" class="link">string</a> <code class="literal">&quot;&quot;</code>,
     and the <a href="language.types.string.php" class="link">string</a> <code class="literal">&quot;0&quot;</code>
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     an <a href="language.types.array.php" class="link">array</a> with zero elements
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     the unit type <a href="language.types.null.php" class="link">NULL</a> (including
     unset variables)
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     Internal objects that overload their casting behaviour to bool.
     For example: <a href="ref.simplexml.php" class="link">SimpleXML</a> objects
     created from empty elements without attributes, or
     <span class="classname"><a href="class.gmp.php" class="classname">GMP</a></span> objects representing the value
     <code class="literal">0</code>.
    </span>
   </li>
  </ul>

  <p class="para">
   Every other value is considered <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>
   (including <a href="language.types.resource.php" class="link">resource</a>
   and <strong><code><a href="math.constants.php#constant.nan">NAN</a></code></strong>).
  </p>

  <div class="warning"><strong class="warning">Warning</strong>
   <p class="simpara">
    <code class="literal">-1</code> is considered <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>, like any other non-zero
    (whether negative or positive) number!
   </p>
  </div>

  <div class="example" id="example-1">
   <p><strong>Example #1 Casting to Boolean</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />var_dump</span><span style="color: #007700">((bool) </span><span style="color: #DD0000">""</span><span style="color: #007700">);        </span><span style="color: #FF8000">// bool(false)<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((bool) </span><span style="color: #DD0000">"0"</span><span style="color: #007700">);       </span><span style="color: #FF8000">// bool(false)<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((bool) </span><span style="color: #0000BB">1</span><span style="color: #007700">);         </span><span style="color: #FF8000">// bool(true)<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((bool) -</span><span style="color: #0000BB">2</span><span style="color: #007700">);        </span><span style="color: #FF8000">// bool(true)<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((bool) </span><span style="color: #DD0000">"foo"</span><span style="color: #007700">);     </span><span style="color: #FF8000">// bool(true)<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((bool) </span><span style="color: #0000BB">2.3e5</span><span style="color: #007700">);     </span><span style="color: #FF8000">// bool(true)<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((bool) array(</span><span style="color: #0000BB">12</span><span style="color: #007700">)); </span><span style="color: #FF8000">// bool(true)<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((bool) array());   </span><span style="color: #FF8000">// bool(false)<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((bool) </span><span style="color: #DD0000">"false"</span><span style="color: #007700">);   </span><span style="color: #FF8000">// bool(true)<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>

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