A boolean has only two possible values: true or false. Booleans are the answer to a yes-or-no question, and they drive every decision in your code.
<?php
$isLoggedIn = true;
$hasPaid = false;
var_dump($isLoggedIn, $hasPaid);
bool(true)
bool(false)
Comparisons produce booleans
<?php
$age = 20;
var_dump($age >= 18);
var_dump($age === 30);
bool(true)
bool(false)
Printing a boolean
echo true prints 1, and echo false prints nothing at all. That surprises many beginners.
<?php
echo "[" . true . "]\n";
echo "[" . false . "]";
[1]
[]
To see the real value, use var_dump(), or write your own words.
<?php
$active = false;
echo $active ? "Active" : "Inactive";
Inactive
What counts as false?
When PHP needs a yes or no (for example in an if), it converts the value to a boolean. These values become false, and everything else becomes true:
false0and0.0""(empty string) and"0"[](empty array)null
<?php
var_dump((bool) "hello");
var_dump((bool) "0");
var_dump((bool) []);
var_dump((bool) "false");
bool(true)
bool(false)
bool(false)
bool(true)
Notice the last line: the text "false" is a non-empty string, so it is true.
The string "false" is true
"false", if ($value) treats it as true. Compare the text, or use filter_var($value, FILTER_VALIDATE_BOOLEAN).