PHP Tutorial - PHP Boolean






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.

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


<?PHP
    $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/* w ww  .  j a  va 2s  . co  m*/
$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.