DEV Community

Salma
Salma

Posted on • Updated on

Decoding PHP's Empty Value Functions: When to Use Which

In PHP, checking for empty values is a common operation, and there are several ways to do this. Understanding the differences between using !, is_null(), and isset() is important, as they each check for different conditions:

Using ! (Not Operator)

The not operator ! is used to check if a value is false. In PHP, several types of values are considered as "falsy" when used in a boolean context:

  • The boolean false itself
  • The integer 0
  • The float 0.0
  • The empty string '' and the string "0"
  • An array with zero elements
  • The special type NULL
  • SimpleXML objects created from empty tags

Example:

$value = '';
if (!$value) {
    echo 'The value is considered empty.';
}
// This will output: The value is considered empty.
Enter fullscreen mode Exit fullscreen mode

Using is_null()

The is_null() function checks if a variable is exactly NULL. It does not check for any other "empty" values like '' (empty string) or 0 (zero as an integer or float), it only returns true if the variable is NULL.

Example:

$value = NULL;
if (is_null($value)) {
    echo 'The value is NULL.';
}
// This will output: The value is NULL.
Enter fullscreen mode Exit fullscreen mode

Using isset()

The isset() function checks if a variable is set and is not NULL. If a variable has been unset using unset(), has not been assigned a value, or is assigned the value NULL, then isset() will return false. It is often used to check if array keys or object properties exist and are not NULL.

Example:

$value = NULL;
if (isset($value)) {
    echo 'This will not be printed.';
} else {
    echo 'The variable is not set or is NULL.';
}
// This will output: The variable is not set or is NULL.
Enter fullscreen mode Exit fullscreen mode

isset() also returns true for variables set to any value other than NULL, including false, 0, or an empty string ''. However, isset() can be used to check for the existence of a variable while is_null() and the not operator can only be used on variables that are already set.
The empty() function in PHP is used to determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value is one of the following:

  • "" (an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • "0" (0 as a string)
  • null
  • false
  • An array with zero elements

It's important to note that empty() is a bit more comprehensive in its checks compared to using the ! operator. While the ! operator will return true for any falsy value, empty() will also return true for unset variables (variables that have not been declared), which the ! operator will not do, since using ! on an unset variable will raise a notice.

Here's an example illustrating the use of empty():

$value = 0;

if (empty($value)) {
    echo 'The value is considered empty by empty().';
}
// This will output: The value is considered empty by empty().

if (!$value) {
    echo 'The value is considered empty by ! operator.';
}
// This will also output: The value is considered empty by ! operator.
Enter fullscreen mode Exit fullscreen mode

It's important to use empty() when you expect the variable might not be set at all, as it won't generate a warning or notice, whereas ! will if you use it on a variable that hasn't been declared.

empty() is particularly useful when you need to check for the presence and non-empty value of a variable (like user input or a potentially missing array key) in a single operation.

Here's a comparison example with isset():

$value = "";

// This will return true because $value is an empty string.
if (empty($value)) {
    echo 'The value is empty.';
}

// This will return false because $value is set.
if (!isset($value)) {
    echo 'The variable is not set.';
} else {
    echo 'The variable is set.';
}
Enter fullscreen mode Exit fullscreen mode

In the above example, even though $value is an empty string and considered "empty", it is still set, so isset() will return true.

**

More Examples :

**
Here are four examples each for empty(), ! (Not operator), is_null(), and isset():

empty()

// Example 1: Variable is an empty string
$var1 = "";
echo empty($var1) ? 'empty' : 'not empty'; // Outputs 'empty'

// Example 2: Variable is zero as an integer
$var2 = 0;
echo empty($var2) ? 'empty' : 'not empty'; // Outputs 'empty'

// Example 3: Variable is false
$var3 = false;
echo empty($var3) ? 'empty' : 'not empty'; // Outputs 'empty'

// Example 4: Variable is an empty array
$var4 = array();
echo empty($var4) ? 'empty' : 'not empty'; // Outputs 'empty'
Enter fullscreen mode Exit fullscreen mode

! (Not operator)

// Example 1: Variable is an empty string
$var1 = "";
echo !$var1 ? 'true' : 'false'; // Outputs 'true'

// Example 2: Variable is zero as an integer
$var2 = 0;
echo !$var2 ? 'true' : 'false'; // Outputs 'true'

// Example 3: Variable is false
$var3 = false;
echo !$var3 ? 'true' : 'false'; // Outputs 'true'

// Example 4: Variable is null
$var4 = null;
echo !$var4 ? 'true' : 'false'; // Outputs 'true'
Enter fullscreen mode Exit fullscreen mode

is_null()

// Example 1: Variable is explicitly set to null
$var1 = null;
echo is_null($var1) ? 'null' : 'not null'; // Outputs 'null'

// Example 2: Variable has not been set at all
unset($var2); // Assuming $var2 was set before
echo is_null($var2) ? 'null' : 'not null'; // Outputs 'null'

// Example 3: Variable is an empty string (not null)
$var3 = "";
echo is_null($var3) ? 'null' : 'not null'; // Outputs 'not null'

// Example 4: Variable is zero (not null)
$var4 = 0;
echo is_null($var4) ? 'null' : 'not null'; // Outputs 'not null'
Enter fullscreen mode Exit fullscreen mode

isset()

// Example 1: Variable is set to non-null value
$var1 = "Hello, World!";
echo isset($var1) ? 'set' : 'not set'; // Outputs 'set'

// Example 2: Variable is set to null (therefore not set)
$var2 = null;
echo isset($var2) ? 'set' : 'not set'; // Outputs 'not set'

// Example 3: Variable is set to an empty array
$var3 = array();
echo isset($var3) ? 'set' : 'not set'; // Outputs 'set'

// Example 4: Variable has not been set at all
echo isset($var4) ? 'set' : 'not set'; // Outputs 'not set'
Enter fullscreen mode Exit fullscreen mode

In summary:
Each function has its specific use case: empty() checks if the variable is considered "empty" in a broader sense; ! checks for "falsey" values; is_null() checks if the variable is null; and isset() checks if a variable is set and not null.

Top comments (1)

Collapse
 
alesrosa profile image
Alessandro Rosa • Edited

I have submitted an RFC to php.net last week, where I suggested why a better approach to empty() function should be followed and how it could be implemented