PHP empty() Function

PHP empty() checks whether a variable is empty. It returns true when the variable does not exist, or when it has a “false-like” value such as 0, empty string or NULL.

It is mostly used in form validation, for example to find out if the user left a field blank.


Syntax

empty(mixed $var): bool

Parameters

ParameterDescription
varRequired. The variable or expression you want to check

Return value: true if the value is empty, otherwise false. empty() does not give any warning when the variable does not exist.


Which values are empty?

All the values in this table are considered empty by PHP.

Valueempty() result
“” (empty string)true
0 (integer)true
0.0 (float)true
“0” (string with zero)true
NULLtrue
falsetrue
[] (empty array)true
Undefined variabletrue
“0.0” (string)false
” ” (a single space)false
“Hello”false
[0] (array with one item)false
Note: The string "0" is empty in PHP. This surprise a lot of beginners, so be careful when 0 can be a valid input (like quantity or marks).

Examples

Example #1: Check simple values

<?php
$a = "";
$b = 0;
$c = "Hello";

var_dump(empty($a));
var_dump(empty($b));
var_dump(empty($c));
?>

Output:

Tutorials Class - Output Window

bool(true)
bool(true)
bool(false)


Example #2: Undefined variable

The variable below is never created. empty() still works and gives true without any warning.

<?php
var_dump(empty($notDefined));
?>

Output:

Tutorials Class - Output Window

bool(true)


Example #3: String “0” and arrays

<?php
var_dump(empty("0"));
var_dump(empty([]));
var_dump(empty([0]));
?>

Output:

Tutorials Class - Output Window

bool(true)
bool(true)
bool(false)


Example #4: Form validation

A common use of empty() is to check if a required field is blank.

<?php
$username = "";

if (empty($username)) {
    echo "Please enter your username.";
} else {
    echo "Welcome, " . $username;
}
?>

Output:

Tutorials Class - Output Window

Please enter your username.


isset() vs empty()

Valueisset()empty()
Undefined variablefalsetrue
NULLfalsetrue
“” (empty string)truetrue
0truetrue
“Hello”truefalse

Common mistake: 0 is a valid value

Suppose a shop allows quantity 0. If you check it with empty(), PHP says it is empty and you show a wrong error message.

<?php
$quantity = 0;

// Wrong: 0 is a valid quantity but empty() returns true
if (empty($quantity)) {
    echo "Please enter quantity.";
}
?>

Output:

Tutorials Class - Output Window

Please enter quantity.

A better way is to check with isset() and compare with an empty string.

<?php
$quantity = 0;

if (!isset($quantity) || $quantity === "") {
    echo "Please enter quantity.";
} else {
    echo "Quantity is " . $quantity;
}
?>

Output:

Tutorials Class - Output Window

Quantity is 0


Summary

  • empty() returns true for “”, 0, “0”, NULL, false, [] and undefined variables.
  • It never gives a warning for undefined variable.
  • empty($x) is same as !isset($x) || $x == false.
  • Do not use empty() when 0 or “0” is a valid value.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.