Truthy and Falsy Values in PHP: A Complete Guide

Updated: January 10, 2024 By: Guest Contributor Post a comment

Introduction

Understanding truthy and falsy values in PHP is crucial for making informed decisions in conditional statements and expressions. This guide provides a thorough look into how PHP determines the truthiness or falsiness of different values.

What Are Truthy and Falsy Values?

In PHP, a value is considered truthy if it evaluates to TRUE in a boolean context, and falsy if it evaluates to FALSE. All values are truthy except for several well-defined falsy ones: the boolean FALSE, integer 0, float 0.0, empty string '' or "", string '0', NULL, an array with zero elements, and the special type unset.

Understanding this concept is important for control structures like if statements and ternary operators, as well as functions that return boolean values or require boolean type parameters.

Basic Examples

Let’s start with some basic examples:

// Falsy values
var_dump((bool) '');        // bool(false)
var_dump((bool) 0);         // bool(false)
var_dump((bool) 0.0);       // bool(false)
var_dump((bool) '0');       // bool(false)
var_dump((bool) NULL);      // bool(false)
var_dump((bool) array());   // bool(false)

// Truthy values
var_dump((bool) 'Hello');   // bool(true)
var_dump((bool) 1);         // bool(true)
var_dump((bool) -2);        // bool(true)
var_dump((bool) 2.3);       // bool(true)
var_dump((bool) array(1));  // bool(true)
var_dump((bool) 'false');   // bool(true)

Conditional Statements

Now, let’s see some conditional statements in action:

if ('hello') {
    echo 'This is truthy.';
} else {
    echo 'This is falsy.';
}

// Outputs: This is truthy.

if (0) {
    echo 'This is truthy.';
} else {
    echo 'This is falsy.';
}

// Outputs: This is falsy.

Note that a non-empty string even containing the text ‘false’ is considered truthy, because it’s not one of the special falsy values.

Working with Functions

PHP functions often rely on truthy and falsy values. Here’s an example:

function isEmpty($value) {
    return !$value;
}

echo isEmpty('');  // Outputs: 1 (true)
echo isEmpty('PHP'); // Outputs: (no output because it's false)

This simple function illustrates how returning a negated value can help determine if a value is falsy (i.e., empty or not set).

Advanced Truthy and Falsy Checks

In more advanced scenarios, you might be dealing with objects or checking for variable existence. Let’s explore:

// Objects are always truthy
class MyClass {}
$myObject = new MyClass();
if ($myObject) {
    echo 'This object is truthy.';
}

// Outputs: This object is truthy.

// Using isset() to check for variable existence
$notSet;
if (isset($notSet)) {
    echo 'This variable is set and truthy.';
} else {
    echo 'This variable is not set or falsy.';
}

// Outputs: This variable is not set or falsy.

Here, even when a newly created object holds no properties or methods, it is still considered truthy.

The isset() and empty() Functions

The isset() and empty() functions are commonly used to check for variable status in PHP. Here’s what you should know:

// isset() checks if a variable is set and is not NULL
$myVar = '';
var_dump(isset($myVar)); // bool(true)

// empty() checks if a variable is 'empty,'
// which includes both falsy values and undefined variables
var_dump(empty($myVar)); // bool(true)
var_dump(empty($notDefinedVar)); // bool(true)

isset() is particularly useful when you want to check if a variable exists without triggering an error if it doesn’t. While empty() can be used to check for a value that is falsy or not yet set.

Strict Comparisons

In PHP, using == for comparison allows for type juggling, which might lead to unexpected truthy or falsy evaluations. Use === for strict comparisons:

if (0 == '0') {
    echo 'Truthy comparison.';
} else {
    echo 'Falsy comparison.';
}

// Outputs: Truthy comparison.

if (0 === '0') {
    echo 'Truthy comparison.';
} else {
    echo 'Falsy comparison.';
}

// Outputs: Falsy comparison.

Conclusion

In this guide, we have learned about truthy and falsy values in PHP, explored basic to advanced examples, and emphasized the importance of understanding these concepts for controlling the flow of your PHP code. This knowledge will surely aid in your development journey, allowing you to write more robust and error-proof code.