PHP empty() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Variables

Definition

In PHP, empty() checks whether a variable is empty. It returns true when the variable does not exist or holds a falsy value, and false otherwise.

empty() is a language construct, so it never raises a warning for an undefined variable or a missing array key. We still write it with parentheses like a function. The values it counts as empty are "", "0", 0, 0.0, null, false and an empty array.

Syntax

Call empty() with the values you need. Start with this line.

PHP
$isEmpty = empty($var);
CallWhat it does
empty($name)Returns true if $name is missing, "", "0", 0, 0.0, null, false or [].
empty($user['email'])Returns true if the key is missing or its value is falsy, with no warning.
!empty($list)Returns true when $list exists and has something in it.

empty($x) is the same as !isset($x) || !$x, but shorter. Because it counts 0 and "0" as empty, it can give surprises when 0 is a valid value, such as a quantity or a score.

Library declarations
PHP
// empty() is a language construct, not a regular function
empty(mixed $var): bool

Parameters

ParameterDescription
varRequired. The variable or expression to check. Missing variables and missing array keys are allowed, and do not cause a warning.

Return Value

Return ValueDescription
booltrue if the variable does not exist or is falsy ("", "0", 0, 0.0, null, false, []). false if it holds any other value.

PHP Examples

Example 1 — Checking Simple Variables

Test four variables that hold different values and print each result.

PHP
<?php
$a = 0;
$b = "hello";
$c = "";
$d = [1];

var_dump(empty($a));
var_dump(empty($b));
var_dump(empty($c));
var_dump(empty($d));

How It Works

  1. Four variables are created: a zero, a word, an empty string and an array with one item.
  2. empty($a) is true, because 0 is a falsy value.
  3. empty($b) is false, because "hello" is a non-empty string.
  4. An empty string is true, and an array that holds an item is false.

Example 2 — Which Values Count as Empty

This loop tests the full list of empty values, and two look-alikes that are not empty.

PHP
<?php
$tests = [
    ['"0"', "0"],
    ['0', 0],
    ['0.0', 0.0],
    ['""', ""],
    ['null', null],
    ['false', false],
    ['[]', []],
    ['"0.0"', "0.0"],
    ['" "', " "],
];

foreach ($tests as [$label, $value]) {
    echo $label, " => ", empty($value) ? "empty" : "not empty", PHP_EOL;
}

How It Works

  1. Each test is a pair: a label to print and the real value to check.
  2. The foreach loop unpacks every pair into $label and $value.
  3. The first seven values ("0", 0, 0. 0, "", null, false and []) are all reported as empty.
  4. The strings "0. 0" and " " are not empty, because only the exact string "0" and the empty string count as empty.

Example 3 — No Warnings for Missing Data

empty() is safe to use on variables and array keys that were never set.

PHP
<?php
var_dump(empty($missing));

$user = ['name' => 'Asha'];
var_dump(empty($user['email']));
var_dump(empty($user['name']));

$cart = [];
echo empty($cart) ? "Cart is empty" : "Cart has items", PHP_EOL;

How It Works

  1. $missing was never created, but empty() simply returns true. Other functions would warn about an undefined variable.
  2. The key 'email' does not exist in $user, so empty() returns true without any warning.
  3. The key 'name' holds "Asha", so empty() returns false.
  4. An empty array means a cart with no items, so the ternary prints "Cart is empty".

Frequently Asked Questions

empty() returns true when a variable is not set or holds a falsy value ("", "0", 0, 0.0, null, false or an empty array). It returns false for everything else.
isset() returns true when a variable exists and is not null. empty() returns true when a variable is missing or falsy. They differ for values like 0, "0", "" and false: a variable holding 0 is set (isset() is true) but also empty (empty() is true). A variable holding null is not set (isset() is false) and is empty. Use isset() to ask "does it exist and have a value?" and empty() to ask "is it missing or falsy?".
No. It is a language construct, like isset() and unset(). You write it with parentheses, but it cannot be used as a callback such as array_filter($list, 'empty').
No. empty() is built to be safe. It returns true for a variable or array key that does not exist and does not raise a warning or notice.
PHP treats the string "0" as falsy when it converts it to a boolean. This catches many beginners out, for example when 0 is a valid quantity. If 0 is allowed, use isset($x) && $x !== "" instead.

Next: floatval()

floatval() converts a value to a floating-point number (a number with decimals) and return.

floatval() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful