26 Aug 09 PHP Programming Tutorial: Booleans – True or False



PHP booleans are something that simply means TRUE or FALSE. This is very useful when using logic with PHP. For example, if you asked PHP to tell you if 2 is greater than 1, it would return a value of TRUE, because 2 is greater than 1. Another example is to ask PHP if today is a Tuesday using the date function. It will return FALSE on six out of seven days of the week.

PHP also considers the following values to be FALSE:

  • An empty string
  • 0 or “0″ or 0.0
  • NULL (essentially means “nothing”)
  • The string FALSE (upper and lowercase)

Any other values in a Boolean variable are TRUE. Here’s a quick example:

<?php
$true = true;
$false = false; // These words do not need to be in quotations. They are not strings, they are logical operators.
 
echo "True: $true"; // this will echo true. The PHP output for true is the number 1.
echo "<br />";
echo "False: $false"; // this will echo false. The PHP outfut for false is NULL (nothing).
?>

Go back to Building PHP Scripts.

Leave a Comment