PHP – Lesson 05: Booleans and NULL

Booleans

True vs. False

A Boolean is a data type with either one of two values: true or false. These values, true and false, are NOT strings; they are instead logical operators which are case insensitive and are declared without quotations. They represent the concept of true or false, yes or no, 0 or 1, on or off (an so forth).

Booleans are generally used in conditional tests to see which decision PHP needs to take based on the outcome of the test.

NULL

NULL, also case insensitive, is a concept representing nothing or an empty value. It does not have a companion opposite value, though, as do Booleans. While Booleans are typically used for testing one state or the other, NULL is typically just a value indicating the absence of something, especially when PHP is accessing a database. While NULL is very similar to the false Boolean value, it is not the same.


But WAIT! A little bit about the Boolean type:

Boolean data types represent two possible states — TRUE or FALSE. Boolean values
are used mainly to compare conditions for use in conditional statements. For example, when PHP evaluates an expression, such as $a > $b (or in plain English, “is $a greater than $b?”), the Boolean outcome is either TRUE or FALSE.

PHP considers the following values FALSE :

  • The actual word, FALSE outside of quotes ( which can be upper- or lowercase)
  • The integer 0
  • The float 0.0
  • An empty string
  • The one-character string 0
  • The constant NULL

Any other values in a Boolean variable are considered TRUE. If you echo a Boolean variable, the value FALSE displays as a blank string; the value TRUE echoes as a 1. Functions often return a Boolean variable that you can test to see whether the function succeeded or failed.