empty()

Description

The empty() function tests an expression to determine if it exists and has a non-false value. If passed a variable that doesn’t exist, the empty() function will not generate an error.

Syntax


bool empty( mixed $expr )

Parameters

$expr
The variable to be checked.

Return Value

bool
  • true if the expression doesn’t exist or does exist but is false
  • false if the expression exists and has a value that is not false

Examples

1. What’s Considered Empty?

This example creates variables of various types and assigns them values that the empty() function considers to be “empty.” It then passes each variable to the empty() function and displays the return value.

<?php
$nullString      = "";
$zeroIntString   = "0";
$zeroInt         = 0;
$zeroFloat       = 0.0;
$nullVal         = NULL;
$falseBool       = false;
$emptyArray      = array();
$notAssigned;

echo 'empty( $nullString ):    ' . empty( $nullString )    . "\n";
echo 'empty( $zeroIntString ): ' . empty( $zeroIntString ) . "\n";
echo 'empty( $zeroInt ):       ' . empty( $zeroInt )       . "\n";
echo 'empty( $zeroFloat ):     ' . empty( $zeroFloat )     . "\n";
echo 'empty( $nullVal ):       ' . empty( $nullVal )       . "\n";
echo 'empty( $falseBool ):     ' . empty( $falseBool )     . "\n";
echo 'empty( $emptyArray ):    ' . empty( $emptyArray )    . "\n";
echo 'empty( $notAssigned ):   ' . empty( $notAssigned )   . "\n";
?>

The empty() function returns true (displayed as “1” by echo()) for each passed variable. Strings containing null values or the integer 0 are considered empty. Integers with value 0 and floats with value 0.0 are empty. Variables assigned the NULL value are also empty as are Boolean false and empty arrays. Finally, a variable that is declared but not assigned a value is considered empty.

empty( $nullString ):    1
empty( $zeroIntString ): 1
empty( $zeroInt ):       1
empty( $zeroFloat ):     1
empty( $nullVal ):       1
empty( $falseBool ):     1
empty( $emptyArray ):    1
empty( $notAssigned ):   1

2. Example heading

Description of example….



See Also

References