The Empty Construct

The empty construct checks if a variable is empty. It returns true if the variable does not exist or contains an "empty" value. The variable being checked is passed as the first parameter.

Syntax

phpempty(mixed $var): bool

Example

Let's check several variables for emptiness:

php<?php
  $var1 = '';
  $var2 = 0;
  $var3 = null;
  
  var_dump(empty($var1)); // true
  var_dump(empty($var2)); // true 
  var_dump(empty($var3)); // true
?>

Code execution result:

phptrue
true
true

Example

Let's check non-empty variables:

php<?php
  $var1 = 'text';
  $var2 = 1;
  $var3 = ['a'];
  
  var_dump(empty($var1)); // false
  var_dump(empty($var2)); // false
  var_dump(empty($var3)); // false
?>

Code execution result:

phpfalse
false
false

Example

Let's check a non-existent variable:

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

Code execution result:

phptrue

See Also

  • the isset function,
    which checks for the existence of a variable
  • the is_null function,
    which checks if a variable is null