PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<TypesIntegers>
view the version of this page
Last updated: Thu, 21 Aug 2003

Booleans

This is the easiest type. A boolean expresses a truth value. It can be either TRUE or FALSE.

Note: The boolean type was introduced in PHP 4.

Syntax

To specify a boolean literal, use either the keyword TRUE or FALSE. Both are case-insensitive.

<?php
$foo = True; // assign the value TRUE to $foo
?>

Usually you use some kind of operator which returns a boolean value, and then pass it on to a control structure.

<?php
// == is an operator which test
// equality and returns a boolean
if ($action == "show_version") {
    echo "The version is 1.23";
}

// this is not necessary...
if ($show_separators == TRUE) {
    echo "<hr>\n";
}

// ...because you can simply type
if ($show_separators) {
    echo "<hr>\n";
}
?>

Converting to boolean

To explicitly convert a value to boolean, use either the (bool) or the (boolean) cast. However, in most cases you do not need to use the cast, since a value will be automatically converted if an operator, function or control structure requires a boolean argument.

See also Type Juggling.

When converting to boolean, the following values are considered FALSE:

Every other value is considered TRUE (including any resource).

Warning

-1 is considered TRUE, like any other non-zero (whether negative or positive) number!

<?php
echo gettype((bool) "");        // bool(false)
echo gettype((bool) 1);         // bool(true)
echo gettype((bool) -2);        // bool(true)
echo gettype((bool) "foo");     // bool(true)
echo gettype((bool) 2.3e5);     // bool(true)
echo gettype((bool) array(12)); // bool(true)
echo gettype((bool) array());   // bool(false)
?>



add a note add a note User Contributed Notes
Booleans
adama at NO-UBE dot augustinefam dot org
24-May-2003 12:13
Just a correction to the 29-Apr-2003 10:49 posting. The first section of the comment, where

 $myBool = true ;  print( $myBool ) ;
 is indistinguishable from
 print( "" ) ;
 likewise,
 $myBool = false ; print( $myBool ) ;
 is indistinguishable from
 print( 1 ) ;

is backwards. It should actually be

 $myBool = true ;  print( $myBool ) ;
 is indistinguishable from
 print( 1 ) ; //corrected
 likewise,
 $myBool = false ; print( $myBool ) ;
 is indistinguishable from
 print( "" ) ; //corrected

The posting makes a good point, though. Don't expect the contents of a string to mean something special. As the documentation states, any non-empty string is true, including "false" since it is simply a non-empty string.
discord at spambegone dot mac dot com
06-May-2003 02:37
/* Automatic boolean conversion in php is in line
    with most other programming languages. However,
    sometimes you may need more flexiblity with the
    type conversion. Here's an easy way:

      Say you want the user to be able to type
    any affirmative or negative and have it
    recognised as a boolean... you might do this:
*/

$bool = strtolower($_GET['user_response']);

$bool = ($bool=='yes' or $bool=='true'   or $bool=='correct'
       or $bool=='on'  or $bool=='right'  or $bool=='positive'
       or $bool=='yup' or $bool=='uh-huh' or $bool=='sure'
       or $bool===true);

/* this will set it to true if any of these strings
    are entered, or if it's already true; otherwise,
    it converts it to false. Note the triple equal sign
    when comparing it to the unquoted boolean true:
    this is the "exactly equal to" operator, which
      checks not just for equality, but for type. Using it
    ensures that $bool doesn't get converted to a boolean
    for the comparison: $bool===true is only true
    if $bool is a true boolean, whereas $bool==true
    is true for any non-empty string (except "0").
*/
rachel at NO-UBE dot xtreme dot com
30-Apr-2003 11:49
If you aren't sure whether your variable is actually a boolean or not, be very careful with it.  For whatever reason, a true boolean is stored as the number 1, and a false boolean is stored as nothing, null, empty string, or whatnot.  Thus,

$myBool = true ;  print( $myBool ) ;

is indistinguishable from

print( "" ) ;

likewise,

&myBool = false ; print( $myBool ) ;

is indistinguishable from

print( 1 ) ;

In addition to the above warnings about boolean evaluation, note that testing for equality is particularly unintuitive.  In particular, "false" the string will equate to true.  For example,

$myTrueBool = true ;
if( $myTrueBool == "true" ) print("it's true!") ;
if( $myTrueBool == "false" ) print("it's false!") ;
if( $myTrueBool == "mushroom" ) print("it's a mushroom!") ;
if( $myTrueBool == 4 ) print("it's the number 4!") ;

will print out

it's true!
it's false!
it's a mushroom!
it's the number 4!

But if $myTrueBool were false, instead, you'd get nothing at all.  Caveat Usor.

<TypesIntegers>
 Last updated: Thu, 21 Aug 2003
show source | credits | sitemap | mirror sites 
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by: http://php.mirrors.ilisys.com.au/
Last updated: Sat 01 Nov 2003 04:13:36 EST EST