empty

Checks whether a variable is missing or holds a falsy value such as 0, "", null or [].

Signature
empty(mixed $var): bool
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$varmixedThe variable to be checked. No warning is generated if it does not exist.

Return value

booltrue if var does not exist or its value is falsy, false otherwise.

Errors

empty() is a language construct, not a function: it cannot be used as a callback, with named arguments, or via variable functions.

empty($var) is shorthand for !isset($var) || $var == false. It is true for "", "0", 0, 0.0, false, null, [], and for variables that do not exist at all — and, like isset(), it never emits an undefined-variable or undefined-key warning.

When to reach for it

It is the right tool for optional user input, where "absent" and "submitted blank" should be handled the same way: if (empty($_POST['nickname'])) covers both without two checks. It also reads clearly for collection checks — empty($rows) instead of count($rows) === 0.

What trips people up

The string "0" is empty. This is the classic bug: a valid quantity of zero, a page number, a checkbox value or a legitimate ID of "0" gets silently rejected. When zero is a real value, use isset() or a strict comparison like $v !== '' instead.

Whitespace is not empty — empty(" ") is false, so trim before checking user text. And an object with no properties is not empty either; empty(new stdClass()) returns false. For countable objects, use count().

On inaccessible object properties empty() calls __isset() first, then __get(), so classes with magic access need both defined.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Treating an omitted and a blank-submitted optional form field identically
  • Short-circuiting a template loop when a result set has no rows
  • Falling back to a default when a config string was left unfilled

Frequently asked questions

What is the difference between empty and isset?

isset() only asks whether the variable exists and is not null; empty() additionally treats 0, "0", "", false and [] as empty. A variable can be set and still be empty.

Why does empty return true for "0"?

Because "0" is falsy in PHP, and empty() is defined as !isset($var) || $var == false. Use isset() plus a strict comparison when zero is a valid value.

Is empty() safe on undefined variables?

Yes. empty() is a language construct that suppresses the undefined-variable and undefined-array-key warnings, which is why it is safe on $_POST fields that may not be submitted.

More examples

Why empty() breaks quantity validation

A submitted quantity of "0" is rejected by empty() but accepted by the strict blank test. When zero is meaningful, compare against '' explicitly instead of reaching for empty().

Example · php
<?php
$form = ['qty' => '0', 'note' => '', 'name' => 'Ada'];

foreach ($form as $field => $value) {
    $byEmpty  = empty($value)          ? 'rejected' : 'accepted';
    $byStrict = ($value === '')        ? 'rejected' : 'accepted';
    printf("%-5s empty():%-9s strict:%s\n", $field, $byEmpty, $byStrict);
}

Trimming before an emptiness check

Whitespace-only strings are not empty to PHP, so a user can satisfy a required field with a single space. Trimming first is what makes the check match the intent. The columns show "yes" when the value survives as non-empty.

Example · php
<?php
$inputs = ['  ', "\n\t", 'Ada', ''];

foreach ($inputs as $raw) {
    $clean = trim($raw);
    printf("%-8s raw:%-3s trimmed:%s\n",
        json_encode($raw),
        empty($raw)   ? 'no' : 'yes',
        empty($clean) ? 'no' : 'yes'
    );
}

Discussion

  • Be the first to comment on this lesson.