Boolean in PHP

I. What is a boolean?

A boolean presents a truth value that can be either true or false.

$complete = true;

Boolean is usually used in a loop or a condition.

if ($complete) {
  // do something
} else {
  // do something else
}

II. False value

We can convert other data types to boolean.

Example

  • Integer 0, -0 = false.
  • Floats 0.0 -0.0 = false.
  • An empty string ““= false.
  • A zero string “0” = false.
  • An empty array [] = false.
  • null = false.

III. Using echo

When a boolean is cast into a string, false becomes an empty string, while true becomes 1.

$complete = true;

echo($complete); // 1
var_dump($complete); // bool(true)