PHP Boolean

Description

Booleans hold either true or false. Behind the scenes, booleans are integers.

False value

In addition, PHP considers the following values to be false :

  • The literal value false
  • The integer zero (0)
  • The float zero ( 0.0 )
  • An empty string ( " " )
  • The string zero ( "0" )
  • An array with zero elements
  • The special type null (including any unset variables)
  • A SimpleXML object that is created from an empty XML tag

All other values are considered true in a Boolean context.

Example 1

We can assign true or false value to a boolean type variable.


<?PHP//from   w  ww . j a va  2 s  .co  m
    $bool = true;
    print "Bool is set to $bool\n";
    $bool = false;
    print "Bool is set to $bool\n";
?>

The code above generates the following result.

Example 2

In the second if statement we compare the integer value to a boolean value.


<?PHP//from w ww.  j a v  a 2  s  .  com
$a=100;
if($a==100) {
    echo "the variable equals 1!\n";
}

if($a==true) {
   echo "the variable is true!";
}
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Data Types »




Array
Array Associative
Array Util
ArrayObject
Data Types
Date
Date Format
DateTime
Number
String
String Escape
String Filter
String HTML
String Type
Timezone