Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 8959

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Booleans</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="language.types.intro.html">Introduction</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="language.types.integer.html">Integers</a></div>
 <div class="up"><a href="language.types.html">Types</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="language.types.boolean" class="sect1">
 <h2 class="title">Booleans</h2>
 
 <p class="simpara">
  This is the simplest type. A <span class="type"><a href="language.types.boolean.html" class="type boolean">boolean</a></span> expresses a truth value. It
  can be either <strong><code>TRUE</code></strong> or <strong><code>FALSE</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.html" class="type boolean">boolean</a></span> literal, use the keywords <strong><code>TRUE</code></strong> or
   <strong><code>FALSE</code></strong>. Both are case-insensitive.
  </p>

  <div class="informalexample">
   <div class="example-contents">
<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, the result of an <a href="language.operators.html" class="link">operator</a>
   which returns a <span class="type"><a href="language.types.boolean.html" class="type boolean">boolean</a></span> value is passed on to a
   <a href="language.control-structures.html" class="link">control structure</a>.
  </p>

  <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">//&nbsp;==&nbsp;is&nbsp;an&nbsp;operator&nbsp;which&nbsp;tests<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;this&nbsp;can&nbsp;be&nbsp;used&nbsp;with&nbsp;exactly&nbsp;the&nbsp;same&nbsp;meaning:<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 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.html" class="type boolean">boolean</a></span>, use the
   <em>(bool)</em> or <em>(boolean)</em> casts. However, in
   most cases the cast is unnecessary, since a value will be automatically
   converted if an operator, function or control structure requires a
   <span class="type"><a href="language.types.boolean.html" class="type boolean">boolean</a></span> argument.
  </p>

  <p class="simpara">
   See also <a href="language.types.type-juggling.html" class="link">Type Juggling</a>.
  </p>
   
  <p class="para">
   When converting to <span class="type"><a href="language.types.boolean.html" class="type boolean">boolean</a></span>, the following values are considered
   <strong><code>FALSE</code></strong>:
  </p>
  
  <ul class="itemizedlist">
   <li class="listitem">
    <span class="simpara">
     the <a href="language.types.boolean.html" class="link">boolean</a> <strong><code>FALSE</code></strong> itself
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     the <a href="language.types.integer.html" class="link">integer</a> 0 (zero)
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     the <a href="language.types.float.html" class="link">float</a> 0.0 (zero)
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     the empty <a href="language.types.string.html" class="link">string</a>, and the
     <a href="language.types.string.html" class="link">string</a> &quot;0&quot;
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     an <a href="language.types.array.html" class="link">array</a> with zero elements
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     an <a href="language.types.object.html" 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.html" class="link">NULL</a> (including
     unset variables)
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     <a href="ref.simplexml.html" class="link">SimpleXML</a> objects created from empty
     tags
    </span>
   </li>
  </ul>
    
  <p class="para">
   Every other value is considered <strong><code>TRUE</code></strong> (including any
   <a href="language.types.resource.html" class="link">resource</a>).
  </p>
  
  <div class="warning"><strong class="warning">Warning</strong>
   <p class="simpara">
    <em>-1</em> is considered <strong><code>TRUE</code></strong>, like any other non-zero
    (whether negative or positive) number!
   </p>
  </div>
  
  <div class="informalexample">
   <div class="example-contents">
<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><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="language.types.intro.html">Introduction</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="language.types.integer.html">Integers</a></div>
 <div class="up"><a href="language.types.html">Types</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>