Skip to content
Theory · Conditions · lesson 3 of 8

Truthiness

In the previous lesson, the if condition was a comparison, and it gave true or false. But PHP lets you put any value into a condition—a number, a string, a variable.

Back to the signpost at the fork. It understands only two answers: "yes" and "no". When the condition holds a string or a number instead of a comparison, PHP first turns the value into true or false and only then picks the road.

PHP
<?php

$name = 'Anna';
if ($name) {
    echo 'Hello, ', $name, "\n"; // Hello, Anna
}
$name = '';
if ($name) {
    echo 'Hello, ', $name, "\n";
} else {
    echo 'Who are you?', "\n"; // Who are you?
}

Let's go through it step by step:

  1. if ($name) does not compare $name with anything: PHP turns the string 'Anna' into a boolean and gets true.
  2. The condition is true, and the program prints Hello, Anna.
  3. After $name = '' the variable holds an empty string, and it turns into false.
  4. So the second if runs its else branch, and the program prints Who are you?.

This check is handy, but its rules are strict: PHP decides by a fixed list, not by meaning.

Falsy values

A value that turns into false in a condition is called a falsy value. Remember the seven falsy values:

  • false: the boolean "no" itself;
  • 0 and 0.0: the integer and the float zero;
  • '': the empty string;
  • '0': a string of one character, the digit zero;
  • []: the empty array, that is, a collection of values with no elements at all;
  • null: "no value".

Everything else is a truthy value: any non-zero number, -1 included, any other string, an array with at least one element. If you have written JavaScript, note this one separately: an empty array is truthy there and falsy in PHP.

Interviewers like to ask for the list of falsy values, and most often about the string '0'.

Trap strings

The (bool) cast shows what a value turns into. It works like (int) and (string) and gives the same result as the condition. echo prints false as nothing, so the result goes through var_dump:

PHP
var_dump((bool) '');      // bool(false)
var_dump((bool) '0');     // bool(false)
var_dump((bool) '0.0');   // bool(true)
var_dump((bool) ' ');     // bool(true)
var_dump((bool) 'false'); // bool(true)

Let's go through it line by line:

  1. The empty string '' is falsy.
  2. The string '0' is falsy, even though it holds a character.
  3. The string '0.0' is truthy: PHP does not read it as a number but looks at the text, and it is neither '' nor '0'.
  4. A string of one space, ' ', is not empty, so it is truthy.
  5. The string 'false' is truthy: to PHP it is five letters, not a boolean.

The rule for strings: exactly two strings are falsy, '' and '0'. Any other string is truthy, even if it looks like a zero or like the word "false".

This is where the signpost analogy falls short. A person at the fork would read "0.0" and "0" the same way, but PHP checks its list: '0' is "no" to it, and '0.0' is "yes".

Common mistake

Values from a form or a settings file arrive in the program as strings. An "administrator" flag came in as the text 'false' and was checked directly:

PHP
$isAdmin = 'false';
if ($isAdmin) { // ✗
    echo 'admin panel', "\n";
} else {
    echo 'no access', "\n";
}

The program prints admin panel: the string 'false' is truthy. There is no error message, and access goes to someone who was never given it. Compare such strings explicitly:

PHP
if ($isAdmin === 'true') {
    echo 'admin panel', "\n";
} else {
    echo 'no access', "\n"; // no access
}

Key points

  • In a condition, PHP turns any value into true or false.
  • The falsy values are false, 0, 0.0, '', '0', [] and null; all other values are truthy.
  • Among strings, only '' and '0' are falsy: '0.0', ' ' and 'false' are truthy.
  • Compare strings from a form explicitly, with ===, instead of checking their truthiness.