W3docs

empty

Learn how PHP's empty() checks if a variable is empty or unset, which values count as empty (the "0" gotcha included), and how it differs from isset().

The PHP empty() Language Construct

empty() is a PHP language construct that tells you whether a variable is empty — that is, whether it either does not exist or holds a value that PHP considers "falsy" (such as "", 0, or null). Because it never emits a warning for undefined variables, it is the go-to tool for checking optional input such as form fields, query parameters, and configuration values.

This page covers the syntax, the exact list of values treated as empty, the common gotchas, and how empty() differs from the related constructs isset() and is_null().

Strictly speaking empty() is a language construct, not a function, even though it is written with parentheses. That distinction has one practical consequence — see It only works on variables below.

Syntax

<?php

if (empty($variable)) {
  // $variable is empty (or does not exist)
}

empty($variable) returns true when the variable is empty and false otherwise. It is logically equivalent to !isset($variable) || $variable == false.

What counts as empty

A variable is empty when it does not exist or holds any of these values:

ValueWhy it is empty
unset / undefined variablethe variable was never assigned
nullthe "no value" type
falsethe boolean false
0 (int) and 0.0 (float)numeric zero
"0" (string)the string containing a single zero — a classic surprise
"" (empty string)a string with no characters
[] (empty array)an array with no elements

Everything else — including "0.0", " " (a space), "false", and the array [0] — is not empty.

The "0" rule is the one that catches people out. A house number, a postal code, or a search term of "0" will be reported as empty. If a literal zero is a valid value, check the length or compare explicitly instead of using empty().

Examples

<?php

// Example 1 — empty string
$myVariable = "";
if (empty($myVariable)) {
  echo "Variable is empty" . PHP_EOL;
} else {
  echo "Variable is not empty" . PHP_EOL;
}

// Example 2 — empty array
$myArray = [];
if (empty($myArray)) {
  echo "Array is empty" . PHP_EOL;
} else {
  echo "Array is not empty" . PHP_EOL;
}

// Output:
// Variable is empty
// Array is empty

A practical use case: validating form input

empty() shines when guarding against missing or blank request data, because it checks for "not set" and "blank" in a single expression:

<?php

$username = $_POST['username'] ?? null;

if (empty($username)) {
  echo "Please enter a username." . PHP_EOL;
} else {
  echo "Welcome, $username!" . PHP_EOL;
}

// With $_POST['username'] missing or "" → "Please enter a username."
// With $_POST['username'] = "Sam"      → "Welcome, Sam!"

The "0" gotcha in action

<?php

$values = ["", "0", "0.0", "hello", " "];

foreach ($values as $value) {
  $state = empty($value) ? "empty" : "not empty";
  echo "'$value' is $state" . PHP_EOL;
}

// Output:
// '' is empty
// '0' is empty
// '0.0' is not empty
// 'hello' is not empty
// ' ' is not empty

Notice that "0" is reported as empty but "0.0" and a single space are not.

It only works on variables

Because empty() is a language construct, it can only be used on variables — passing the result of an expression or a function call is a syntax error before PHP 5.5 and a logic trap to avoid in general:

<?php

// Valid — argument is a variable:
$name = trim("  ");
if (empty($name)) {
  echo "Name is blank";
}

// Avoid — empty(trim($name)) reads awkwardly; assign first, as above.

empty() vs isset() vs is_null()

These three constructs are easy to confuse. The table shows what each returns for the same inputs:

Inputempty()isset()is_null()
undefined variabletruefalsewarning + true
nulltruefalsetrue
""truetruefalse
0 / "0"truetruefalse
falsetruetruefalse
[]truetruefalse
"hello"falsetruefalse

Rules of thumb:

  • Use isset() to ask "does this variable exist and is it not null?" — ideal for array keys and object properties.
  • Use is_null() to ask "is this value exactly null?" and nothing else.
  • Use empty() to ask "is this missing or blank?" — the broadest, most permissive check.

Conclusion

empty() gives you a single, warning-free way to check whether a variable is missing or holds a falsy value, which makes it a natural fit for validating optional input. Keep the "0" rule in mind, remember that it only accepts variables, and reach for isset() or is_null() when you need a narrower test. For more on the values these checks operate on, see PHP Data Types and PHP Variables.

Practice

Practice
What does empty() do in PHP?
What does empty() do in PHP?
Was this page helpful?