W3docs

empty()

The empty() function is a built-in function in PHP that checks whether a variable is empty. A variable is considered empty if it does not exist, or if its value

Introduction

empty() is a built-in PHP language construct that tells you whether a variable is "empty" — that is, whether it holds no meaningful value. It is most commonly used to guard against missing form fields, absent array keys, and uninitialized variables before your code tries to use them.

The key thing to understand is that empty($var) is essentially shorthand for !isset($var) || $var == false. In other words, a variable is empty when it either does not exist or its value is one of PHP's "falsy" values. This page explains exactly which values count as empty, the gotchas that trip people up, and when to reach for empty() versus isset() or is_null().

Syntax

bool empty(mixed $var)

empty() takes a single argument, $var, and returns a boolean: true if the variable is considered empty, false otherwise. Because it is a language construct and not a regular function, it can be used directly on variables that may not exist without triggering a warning.

Note: Prior to PHP 5.5, empty() only accepted a variable. Modern PHP also allows expressions, e.g. empty(trim($name)).

Which values are "empty"?

A variable is empty when it does not exist, or when its value is any of the following:

Valueempty() returns
"" (empty string)true
"0" (string zero)true
0 (integer zero)true
0.0 (float zero)true
nulltrue
falsetrue
[] (empty array)true
undefined variabletrue
any non-empty string (e.g. "hello")false
any non-zero numberfalse
a non-empty arrayfalse

This is exactly the set of values PHP treats as false in a boolean context, plus the case where the variable is unset.

Example Usage

empty() prints nothing for false/0 and 1 for true when echoed, so this example casts the result to an integer label for clarity.

php— editable, runs on the server

Here we define six variables of different types: $var1 is an empty string, $var2 is a non-empty string, $var3 is null, $var4 is 0, $var5 is the string "0", and $var6 is an empty array. empty() returns true for every one except $var2. Note that echo prints 1 for true and an empty string for false; we append 0-vs-1 in the comments to make the result readable.

Common gotchas

"0" is considered empty

This is the single most common surprise. The string "0" is empty according to empty(), even though it clearly contains a character. This matters for form input: a user who types 0 into a quantity or age field will have it rejected if you validate with empty().

<?php
$age = "0";                       // a perfectly valid age from a form
var_dump(empty($age));            // bool(true)  -- oops, treated as missing
var_dump($age === "");            // bool(false) -- the field was NOT blank
?>

If you only want to reject a blank field, compare against "" or use isset() plus a length check instead of empty().

empty() does not warn on undefined variables or keys

Unlike accessing a variable directly, empty() is safe to call on something that may not exist. It returns true and emits no warning.

<?php
$config = ["timeout" => 30];

var_dump(empty($config["timeout"]));  // bool(false) -- key exists, value is 30
var_dump(empty($config["retries"]));  // bool(true)  -- key missing, no warning
var_dump(empty($undeclared));         // bool(true)  -- no "undefined variable" notice
?>

This makes empty() ideal for checking optional array keys, such as $_POST or $_GET values, without first calling isset().

empty() vs isset() vs is_null()

These three are easy to confuse. The difference is which states they treat as "no value":

Variable stateempty()isset()is_null()
not declaredtruefalsewarning + true
nulltruefalsetrue
"", 0, "0", []truetruefalse
"hello", 42falsetruefalse

In short:

  • Use isset() when you only care whether a variable exists and is not null.
  • Use empty() when you want to know whether there is any usable value (and don't care about distinguishing 0/""/missing).
  • Use is_null() when you specifically need to detect null.

A typical real-world guard for an optional form field looks like this:

<?php
$_POST = ["username" => "  "];   // simulate a submitted form with a blank-ish field

if (empty(trim($_POST["username"] ?? ""))) {
    echo "Username is required";
} else {
    echo "Welcome, " . trim($_POST["username"]);
}
?>

Output:

Username is required

Here ?? "" provides a default if the key is missing, trim() collapses whitespace-only input, and empty() then catches both the missing and the blank cases.

Conclusion

empty() is a convenient guard for checking whether a variable holds a meaningful value, and it is safe to use on variables and array keys that may not exist. Its main pitfall is that "0", 0, and empty strings all count as empty, so it is the wrong tool when zero is a legitimate value — reach for isset() or an explicit comparison in those cases. To inspect exactly what a variable contains while debugging, pair it with var_dump() or gettype().

Practice

Practice
In PHP, what characteristics does the 'empty()' function have?
In PHP, what characteristics does the 'empty()' function have?
Was this page helpful?