Variables are a core part of almost every PHP application. Most of the time, variables have initial values, but some variables may be uninitialized, which can trigger PHP warnings about "undefined variables."
There are many reasons why a variable may be undefined: it may not have been declared at all, there could be a typo, or it may be conditionally defined. Additionally, a variable's value may be NULL, which can indicate that it hasn't been initialized or that a function returned an empty value to signal a specific state.
It is crucial to check variables before use to see if they are defined or empty. PHP provides three functions—isset(), empty(), and is_null()—to handle these situations.
Let's understand the basic purpose of these three functions:
isset(): Checks if a variable is declared and not NULL.
empty(): Determines if a variable is either nonexistent or evaluates to false (including 0, empty string, NULL, etc.).
is_null(): Checks whether a variable's value is NULL.
isset() returns true if a variable is defined and not NULL, even if the value is 0, an empty string, or false. On the other hand, empty() returns true if a variable has a "falsy" value, such as 0, empty string "", string "0", an empty array, NULL, or false.
Both are language constructs and cannot be called as variable functions.
<?php $fruit = ''; if(isset($fruit)) { echo 'Do you like '.$fruit.'?'; } // Output: Do you like ? if(!empty($fruit)) { echo 'Do you like '.$fruit.'?'; } // No Output ?>
In fact, empty() can be implemented using isset() with logical operations, but using the built-in empty() function is simpler:
<?php function my_empty($x) { return !isset($x) || $x == false; } ?>
When a variable is explicitly set to NULL, is_null() returns true; otherwise, it returns false. In contrast, isset() returns true as long as the variable is defined and not NULL.
<?php $fruit = NULL; if(isset($fruit)) { echo 'Do you like '.$fruit.'?'; } // No Output if(is_null($fruit)) { echo 'There is no fruit.'; } // Output: There is no fruit. ?>
empty() returns true for "falsy" values, such as empty strings, NULL, 0, or empty arrays, while is_null() only returns true if the value is NULL.
<?php $person = [ 'first_name' => 'Monty', 'last_name' => '', 'age' => '83', 'fav_movie' => NULL ]; if(empty($person['last_name'])) { if(is_null($person['last_name'])) { echo 'The last name is set to NULL.'; } else { echo 'The last name is probably an empty string.'; } } // Output: The last name is probably an empty string. if(is_null($person['fav_movie'])) { echo $person['first_name'].' did not specify a favorite movie.'; } // Output: Monty did not specify a favorite movie. ?>
To write safer and more concise code, consider the following tips:
You can pass multiple variables to isset() at once to check if they are all defined and not NULL.
Avoid using == to check for NULL to prevent false positives from empty strings or false values.
This article explained the usage and differences between PHP's isset(), empty(), and is_null() functions. Understanding these differences helps developers handle variable states more precisely, improving code stability and readability.