search for in the  
<TypesIntegers>
Last updated: Thu, 19 May 2005

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
var_dump
((bool) "");        // bool(false)
var_dump((bool) 1);        // bool(true)
var_dump((bool) -2);        // bool(true)
var_dump((bool) "foo");    // bool(true)
var_dump((bool) 2.3e5);    // bool(true)
var_dump((bool) array(12)); // bool(true)
var_dump((bool) array());  // bool(false)
var_dump((bool) "false");  // bool(true)
?>



User Contributed Notes
Booleans
bioster at peri dot csclub dot uwaterloo dot ca
18-Nov-2004 05:06
If you're *really* concerned with squeezing the most speed out of your code as possible (such as in a highly recursive section), you want to set your boolean style variables to 0 for false and 1 for true.  I've been doing benchmarks and I've found that these are the fastest.

Of course, these operations are so fast that you should only be concerned about the difference when you're iterating over a million or more of these operations.  (We're talking .02 seconds difference with a million iterations on my home machine.)
x123 (at) bestof (dash) inter (dot) net
15-Nov-2004 03:24
another correction to the preceding "correction":
"as the documention states" is right,
"any non-empty string is true" is wrong : (bool) "0" === false !
(but "00" and "0.0" etc. are true!)

So watch out, you can NOT test for non-empty strings using e.g.

<?php
if (is_string($s))
  if (
$s) echo " '$s' is a non-empty string";
  else echo
" '$s' is an empty string"; // wrong for $s='0' !
else echo " $s is not a string ";
?>

indeed, for $s='0' this would print: " '0' is an empty string"
which is wrong, of course.
This is particularly "dangerous" in connection with MySQL
which prints FALSE by default as 0,
which comes back from a FORM POST as string '0',
while PHP prints FALSE as empty string (!);
a similar problem is to preserve a NULL value through a FORM post....
adama at NO-UBE dot augustinefam dot org
23-May-2003 01: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
05-May-2003 03: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").
*/

<TypesIntegers>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 18:35:34 2005 EDT