Skip to content

Bool and null

A bool is true or false. null means “no value”, and only a type that starts with ? can be null. The page Nullable types explains ? and ??.

Truthy and falsy values

A condition can test a value of any type. The value is either truthy or falsy.

The conditions are the test of an if, a while and the middle part of a for. The test of a ternary ? : and of ?: is a condition too, and so are the values beside &&, || and !. These values are falsy: false, 0, 0.0, "", "0", an empty array, an empty bytes and null. Every other value is truthy. This includes "0.0", " " and every object.

Good to know: only a condition tests a value this way. A bool variable, a bool parameter and a bool return value need as bool, which uses the same table. empty($x) is true when $x is falsy. isset($x) is true when $x is not null.

The examples below test the fields of a form, show each kind of condition, build the header of an email from settings that may be empty, and convert values with as bool.

What counts as nothing

<?nvs
// Shows which values of a form are falsy in an `if`.
array<mixed> $submitted = [
'name' => 'Ada',
'nickname' => '',
'age' => 0,
'tags' => [],
'note' => '0.0',
'reply' => null,
];
foreach ($submitted as tainted string $field => mixed $value) {
if ($value) {
echo $field, ": filled in\n";
} else {
echo $field, ": left empty\n";
}
}
// The string "0" is falsy. The strings "0.0" and " " are truthy.
array<string> $answers = ['0', '0.0', ' ', ''];
foreach ($answers as string $answer) {
echo "\"", $answer, "\" ", $answer ? 'is truthy' : 'is falsy', "\n";
}
Output
name: filled in
nickname: left empty
age: left empty
tags: left empty
note: filled in
reply: left empty
"0" is falsy
"0.0" is truthy
" " is truthy
"" is falsy

The places that ask

<?nvs
// Shows each kind of condition that tests a value without `as bool`.
array<string> $queue = ['ship order', 'send receipt'];
string $note = '';
// An `if`: the array has elements, so it is truthy.
if ($queue) {
echo "the queue has work\n";
}
// A `while`: the loop stops when the number is `0`.
int $left = 2;
while ($left) {
echo 'still to do: ', $left, "\n";
$left = $left - 1;
}
// The middle part of a `for`: the loop stops when `$tries` is `0`.
for (int $tries = 2; $tries; $tries = $tries - 1) {
echo 'attempt ', $tries, "\n";
}
// The test of a ternary `? :`: the note is empty, so this prints `(none)`.
echo 'note: ', $note ? $note : '(none)', "\n";
// The values beside `&&` and `||`. The right value is tested only when the left
// value does not decide the result.
if ($queue && !$note) {
echo "work to do, and nothing to say about it\n";
}
// `!` gives `true` for a falsy value.
if (!$note) {
echo "the note is empty\n";
}
// A `bool` variable needs `as bool`. It converts the array to `true`.
bool $hasWork = $queue as bool;
echo 'has work: ', $hasWork ? 'yes' : 'no', "\n";
Output
the queue has work
still to do: 2
still to do: 1
attempt 2
attempt 1
note: (none)
work to do, and nothing to say about it
the note is empty
has work: yes

A digest built from what was filled in

<?nvs
// This program builds the header of a summary email from a user's settings.
// Some settings are empty. An empty name is replaced by the email address,
// and an empty list prints no line.
string $displayName = '';
string $email = 'ada@example.com';
array<string> $topics = [];
array<string> $starred = ['billing', 'outages'];
int $unread = 0;
// `?:` returns the left value if it counts as true, and the right value if not.
// `$displayName` is empty, so the result is the email address.
string $who = $displayName ?: $email;
echo 'To: ', $who, "\n";
// An empty array counts as false, so the `Topics` line is not printed.
if ($topics) {
echo 'Topics: ', Core\Str::join($topics, ', '), "\n";
}
if ($starred) {
echo 'Starred: ', Core\Str::join($starred, ', '), "\n";
}
// `&&` checks `$starred` only if `$unread` counts as true.
// `$unread` is `0`, which counts as false, so this prints `Nothing unread`.
if ($unread && $starred) {
echo "Unread mail in a starred topic\n";
} else {
echo "Nothing unread\n";
}
echo $unread ? 'Unread: ' . ($unread as string) : 'All caught up', "\n";
Output
To: ada@example.com
Starred: billing, outages
Nothing unread
All caught up

Converting with as bool

<?nvs
// Shows that `as bool` gives the same result as a condition.
array<int> $empty = [];
?int $none = null;
// Each of these values is falsy, so each line prints `false`.
echo ('0' as bool) ? 'true' : 'false', "\n";
echo (0.0 as bool) ? 'true' : 'false', "\n";
echo ($empty as bool) ? 'true' : 'false', "\n";
echo ($none as bool) ? 'true' : 'false', "\n";
// Each of these values is truthy, so each line prints `true`.
echo ('abc' as bool) ? 'true' : 'false', "\n";
echo ('0.0' as bool) ? 'true' : 'false', "\n";
echo ((-1) as bool) ? 'true' : 'false', "\n";
// `?:` returns the left value when it is truthy. `0` is falsy, so this prints `none`.
echo 0 ?: 'none', "\n";
Output
false
false
false
false
true
true
true
none

Details

Every enum case is truthy, also a case whose value is 0. A value of type mixed is tested by the type of the value it has while the program runs. -0.0 is falsy, like 0.0. An array that contains one 0 is truthy, because it is not empty.

x as bool gives false for every falsy value and true for every other value.

empty($x) gives true for a falsy value, so empty("0") is true. isset($x) tests only for null, so isset("0") is true.

A label of match (true) is compared with true using ==. It is not tested for truthiness. A label of type int does not compile, because an int is never equal to true (E0466).

A value that is not a bool does not compile in a bool variable, a bool parameter or a bool return value (E0401 or E0403). Write as bool there.

  • Do compare with == null when you mean “has no value”. 0 and "" count as false too.
  • Do use as bool when you store the result of a test in a bool variable.
  • Don’t use true as int. It does not compile. Write $flag ? 1 : 0.