ZetCode

PHP empty Keyword

last modified November 25, 2026

The PHP empty keyword tests whether a value is falsy. It returns true for an empty string, the string "0", integer or float zero, null, false, an empty array, and an undefined variable.

This article covers the full list of empty values, the special case of the string zero, undefined variables and keys, the difference from isset and array_key_exists, form validation, expressions, and the rules that keep the construct from becoming a bug.

Basic definitions

empty is a language construct that answers one question: is this value falsy? It returns true for false, 0, 0.0, "0", "", null, an empty array, and a variable that does not exist. Everything else is non-empty.

The name is narrower than the behavior suggests. A value can be non-empty and still useless, and a value can be empty and perfectly valid: zero is a quantity, an empty string can be a deliberate choice, and "0" is a normal string. The construct tests truthiness, not presence, length, or nullness.

The syntax is empty($value). Since PHP 5.5 the argument may be any expression, not only a variable. empty does not emit a warning for an undefined variable, an undefined array key, or an undefined property, which is why it appears in so much request-handling code. It is not a function, so it cannot be used as a callback or called with zero or several arguments.

The isset keyword article covers the presence test, and unset covers removing a variable. This page concentrates on the falsy test.

An empty string

The first example checks a required text field before using it.

empty_string.php
<?php

declare(strict_types=1);

$username = "";

if (empty($username)) {
    echo "Please enter a username\n";
} else {
    echo "Welcome, $username\n";
}

The empty string is falsy, so the check passes and the message is printed. The else branch would run for any non-empty string, including "0", which is the special case covered below. For a required text field, trimming whitespace before the check is usually a good idea.

$ php empty_string.php
Please enter a username

The values that count as empty

The complete list is short, but a few entries surprise people. The next script tests one value of each kind and prints the result in a fixed-width column.

empty_values.php
<?php

declare(strict_types=1);

$values = [
    ['""', ""],
    ['"0"', "0"],
    ['0', 0],
    ['0.0', 0.0],
    ['"0.0"', "0.0"],
    ['" "', " "],
    ['null', null],
    ['false', false],
    ['[]', []],
    ['[0]', [0]],
    ['object', new stdClass()],
];

foreach ($values as [$label, $value]) {
    echo str_pad($label, 10), empty($value) ? "empty" : "not empty", "\n";
}

The empty string, the string "0", integer zero, float zero, null, false, and an empty array are all empty. "0.0", a single space, an array with one element, and an object are not. Objects are never empty, even when they have no properties: empty(new stdClass()) is false.

$ php empty_values.php
""        empty
"0"       empty
0         empty
0.0       empty
"0.0"     not empty
" "       not empty
null      empty
false     empty
[]        empty
[0]       not empty
object    not empty

Zero and the string zero

The string "0" is the only non-empty string that empty treats as empty. A strict comparison sees the same value as a present string.

empty_zero.php
<?php

declare(strict_types=1);

$quantity = "0";

echo empty($quantity) ? "empty says: missing\n" : "empty says: present\n";
echo $quantity === "" ? "strict says: missing\n" : "strict says: present\n";

The two lines disagree because empty treats "0" as falsy while the strict comparison sees a non-empty string. This is the most common source of bugs with empty: a quantity, price, score, or identifier of zero is rejected as missing. Use isset($value) && $value !== "", or validate the type with filter_var, when zero is valid.

$ php empty_zero.php
empty says: missing
strict says: present

Undefined variables and missing keys

One of the practical advantages of empty is that it stays quiet when the value is missing. A direct read of an undefined variable, key, or property would emit a warning.

empty_undefined.php
<?php

declare(strict_types=1);

$config = ["timeout" => 5];
$object = new stdClass();
$object->name = "Ada";

var_dump(empty($missing));
var_dump(empty($config["retries"]));
var_dump(empty($config["timeout"]));
var_dump(empty($object->name));
var_dump(empty($object->age));

None of the five calls emits a warning. The missing variable, the missing key, and the missing property are all empty; the existing values are not. This silence is convenient for optional configuration, but it hides the difference between not provided and provided as zero or empty. When that difference matters, use array_key_exists or property_exists.

$ php empty_undefined.php
bool(true)
bool(true)
bool(false)
bool(false)
bool(true)

empty versus isset

isset and empty answer different questions. isset asks whether a value exists and is not null; empty asks whether it is falsy. The next script compares both on the same data.

empty_isset.php
<?php

declare(strict_types=1);

$data = ["name" => "", "age" => 0, "note" => null];

foreach ($data as $key => $value) {
    printf(
        "%-5s isset=%-3s empty=%-3s\n",
        $key,
        isset($data[$key]) ? "yes" : "no",
        empty($data[$key]) ? "yes" : "no"
    );
}

var_dump(array_key_exists("note", $data));

For name and age, isset is true and empty is true: the keys exist, but their values are falsy. For note, isset is false because the value is null, while array_key_exists is true because the key is present. The right test depends on whether null counts as a value.

$ php empty_isset.php
name  isset=yes empty=yes
age   isset=yes empty=yes
note  isset=no  empty=yes
bool(true)

Validating form data

Request handling is the most common place for empty. Missing keys and empty strings should produce the same user-facing error, while a numeric field needs a different check.

empty_form.php
<?php

declare(strict_types=1);

$_POST = ["name" => "Ada", "email" => ""];

$name = $_POST["name"] ?? "";
$email = $_POST["email"] ?? "";
$errors = [];

if (empty($name)) {
    $errors[] = "name is required";
}

if (empty($email)) {
    $errors[] = "email is required";
}

if ($errors !== []) {
    echo implode("\n", $errors), "\n";
} else {
    echo "Form accepted for $name\n";
}

The name is present, but the email is an empty string, so one error is collected. The ?? operator supplies a default for a missing key without a warning. For text fields, empty(trim($name)) is a reasonable required check; for numeric fields, use filter_var and compare with false, because empty would reject a valid zero. The GET/POST request article covers the request side.

$ php empty_form.php
email is required

Expressions and function results

Since PHP 5.5 the argument does not have to be a variable. Any expression is allowed, including a function call.

empty_expression.php
<?php

declare(strict_types=1);

var_dump(empty(trim(" ")));
var_dump(empty(str_repeat("x", 0)));
var_dump(empty(1 + 1));
var_dump(empty(new stdClass()));

All four arguments are expressions. A trimmed space is empty, a zero-length repeat is empty, and the sum is not. The result is always a boolean; the construct does not return the tested value. This makes empty convenient in conditions but not a way to normalize data.

$ php empty_expression.php
bool(true)
bool(true)
bool(false)
bool(false)

Rules and limitations

empty is simple, but a few rules are easy to forget.

Best practices

Use empty where its truthiness rule matches the domain rule, and use a stricter test where it does not.

Source

PHP manual: empty

PHP manual: isset

PHP manual: array_key_exists

PHP manual: booleans

This article covered the PHP empty keyword: the falsy values it accepts, the string zero, silent checks on undefined data, the difference from isset, form validation, expressions, and the limits of the construct. Continue with PHP isset keyword for the presence test, or read PHP unset keyword and PHP bool keyword for the neighboring ideas.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all PHP tutorials.