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/language.types.boolean.php

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = 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',
  ),
 
'up' =>
  array (
   
0 => 'language.types.php',
   
1 => 'Types',
  ),
 
'prev' =>
  array (
   
0 => 'language.types.intro.php',
   
1 => 'Introduction',
  ),
 
'next' =>
  array (
   
0 => 'language.types.integer.php',
   
1 => 'Integers',
  ),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);

manual_header();
?>
<div id="language.types.boolean" class="sect1">
 <h2 class="title">Booleans</h2>
 
 <p class="simpara">
  This is the simplest type. A <a href="language.types.boolean.php" class="type boolean">boolean</a> expresses a truth value. It
  can be either <b><tt class="constant">TRUE</tt></b> or <b><tt class="constant">FALSE</tt></b>.
 </p>

 <blockquote><p><b class="note">Note</b>:
  <span class="simpara">
   The <a href="language.types.boolean.php" class="type boolean">boolean</a> type was introduced in PHP 4.
  </span>
 </p></blockquote>

 <div id="language.types.boolean.syntax" class="sect2">
  <h3 class="title">Syntax</h3>
  <p class="para">
   To specify a <a href="language.types.boolean.php" class="type boolean">boolean</a> literal, use the keywords <b><tt class="constant">TRUE</tt></b> or
   <b><tt class="constant">FALSE</tt></b>. Both are case-insensitive.
  </p>

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

  </div>

  <p class="para">
   Typically, some kind of <a href="language.operators.php" class="link">operator</a>
   which returns a <a href="language.types.boolean.php" class="type boolean">boolean</a> value, and the 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 programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;==&nbsp;is&nbsp;an&nbsp;operator&nbsp;which&nbsp;test<br />//&nbsp;equality&nbsp;and&nbsp;returns&nbsp;a&nbsp;boolean<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$action&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #DD0000">"show_version"</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"The&nbsp;version&nbsp;is&nbsp;1.23"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;this&nbsp;is&nbsp;not&nbsp;necessary...<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$show_separators&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;hr&gt;\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;...because&nbsp;instead,&nbsp;this&nbsp;can&nbsp;be&nbsp;used:<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$show_separators</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</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 id="language.types.boolean.casting" class="sect2">
  <h3 class="title">Converting to boolean</h3>

  <p class="simpara">
   To explicitly convert a value to <a href="language.types.boolean.php" class="type boolean">boolean</a>, use the
   <i>(bool)</i> or <i>(boolean)</i> casts. However, in
   most cases the cast is unncecessary, since a value will be automatically
   converted if an operator, function or control structure requires a
   <a href="language.types.boolean.php" class="type boolean">boolean</a> argument.
  </p>

  <p class="simpara">
   See also <a href="language.types.type-juggling.php" class="link">Type Juggling</a>.
  </p>
  
  <p class="para">
   When converting to <a href="language.types.boolean.php" class="type boolean">boolean</a>, the following values are considered
   <b><tt class="constant">FALSE</tt></b>:
  </p>
 
  <ul class="itemizedlist">
   <li class="listitem">
    <span class="simpara">
     the <a href="language.types.boolean.php" class="link">boolean</a> <b><tt class="constant">FALSE</tt></b> itself
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     the <a href="language.types.integer.php" class="link">integer</a> 0 (zero)
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     the <a href="language.types.float.php" class="link">float</a> 0.0 (zero)
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     the empty <a href="language.types.string.php" class="link">string</a>, and the
     <a href="language.types.string.php" class="link">string</a> &quot;0&quot;
    </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">
     an <a href="language.types.object.php" class="link">object</a> with zero member
     variables (PHP 4 only)
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     the special type <a href="language.types.null.php" class="link">NULL</a> (including
     unset variables)
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     <a href="ref.simplexml.php" class="link">SimpleXML</a> objects created from empty
     tags
    </span>
   </li>
  </ul>
   
  <p class="para">
   Every other value is considered <b><tt class="constant">TRUE</tt></b> (including any
   <a href="language.types.resource.php" class="link">resource</a>).
  </p>
 
  <div class="warning"><b class="warning">Warning</b>
   <p class="simpara">
    <i>-1</i> is considered <b><tt class="constant">TRUE</tt></b>, like any other non-zero
    (whether negative or positive) number!
   </p>
  </div>
 
  <div class="informalexample">
   <div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />var_dump</span><span style="color: #007700">((bool)&nbsp;</span><span style="color: #DD0000">""</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;bool(false)<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((bool)&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;bool(true)<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((bool)&nbsp;-</span><span style="color: #0000BB">2</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;bool(true)<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((bool)&nbsp;</span><span style="color: #DD0000">"foo"</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;bool(true)<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((bool)&nbsp;</span><span style="color: #0000BB">2.3e5</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;bool(true)<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((bool)&nbsp;array(</span><span style="color: #0000BB">12</span><span style="color: #007700">));&nbsp;</span><span style="color: #FF8000">//&nbsp;bool(true)<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((bool)&nbsp;array());&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;bool(false)<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((bool)&nbsp;</span><span style="color: #DD0000">"false"</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;bool(true)<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

 </div>
</div><?php manual_footer(); ?>
 
show source | credits | sitemap | contact | advertising | mirror sites