Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

empty

Checks that a variable does not exist or holds an "empty" value.

Syntax

empty(mixed $var): bool

Parameters

ParameterTypeDescription
$varmixedThe variable to check.

Return value

bool`true` for a missing variable, `null`, `false`, `0`, `'0'`, an empty string and an empty array.

Examples

Empty values:

<?php
 
var_dump(empty(''));
var_dump(empty([]));
var_dump(empty('text'));

Output:

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

A trap: the string "0" counts as empty too:

<?php
 
$quantity = '0';
 
var_dump(empty($quantity));
var_dump($quantity === '');

Output:

bool(true)
bool(false)

Notes

If zero is a valid value, check more precisely: $value === '' or $value === null.

See also

  • isset — Checks that a variable exists and is not null.
  • is_null — Checks whether a value is null.

Related recipes

In the tutorial

  • Variables — How to declare variables in PHP, name them and interpolate them into strings.

Thanks for reading!

Donate to the site — we work hard for you.