You will use variables in almost every program written in PHP. Most of the time these variables have a value, and we usually create them with an initial value. However, there's always a chance that some of the variables you use are uninitialized. This may cause PHP to issue warnings about the use of undefined variables.
There may be many reasons for undefined variables. The most common problem is that you don't actually define the variable, or you make a typo when reusing it somewhere else. This is just a programming error. However, another possibility that could result in an undefined variable is that it is conditionally defined.
You may also find that the value of a variable is NULL
. This can happen for a variety of reasons. For example, the variable may simply not have been initialized with a value. Or the function might return null to indicate some kind of error.
Regardless, using a variable before defining it or when it has a null value may have unintended consequences. In this tutorial, I will show you how to check if an element is defined and see if it is empty or null.
You can use isset()
, empty()
or is_null()
to check whether one or all of the conditions are true or false.
Let's start with some definitions.
isset()
: You can use isset()
to determine whether a variable has been declared and is different from null
. empty()
: Used to determine whether the variable exists and the value of the variable is not equal to false
. is_null()
: This function is used to check whether the variable is null
. isset()
and empty()
As we can see from the definition, if we previously defined the variable and set its value to something other than NULL
, isset()
will return true
. This can include 0
, the empty string, or false
. On the other hand, empty()
will return true
whenever a variable value is set to evaluate to false
— we call these "false" values. Examples of falsey values include 0
, the empty string ""
and the string "0"
, the empty array, NULL
, or of course is a boolean value false
.
isset()
and empty()
is that they are both language constructs and therefore cannot be called using variable functions.
The code snippet below should explain the difference between the two.
<?php $fruit = ''; if(isset($fruit)) { echo 'Do you like '.$fruit.'?'; } // Output: Do you like ? if(!empty($fruit)) { echo 'Do you like '.$fruit.'?'; } // No Output ?>
Please note that empty()
can be written using the isset()
function:
<?php function my_empty($x) { return !isset($x) || $x == false } ?>
Of course, it's usually easier to use the built-in empty()
function.
isset()
and is_null()
If the value of the variable has been explicitly set to NULL
, the is_null()
function will return true
. Otherwise, it just returns false
. On the other hand, isset()
returns true
whenever a variable is defined and its value is not NULL
.
Here is a basic example to show the difference between them.
<?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()
and is_null()
If the value of the variable evaluates to false
, the empty()
function will return true
. This can mean an empty string, NULL
, an integer 0
, or an array with no elements. On the other hand, is_null()
will return true
only if the value of the variable is NULL
.
Here is a basic example to show the difference between them.
<?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.
There are two tips you can use to write cleaner code and avoid errors in the future.
1. Unlike empty()
and is_null()
, you can pass multiple values to isset()
at once to check whether Any are undefined or set to NULL
. In this case, isset()
will only return true
if none of the passed values are NULL
.
2. Do not use ==
to check if the value is NULL
. This will provide false positives for erroneous values such as the empty string that evaluates to false
.
This tutorial quickly explains the differences between isset()
, empty()
and is_null()
. Hopefully you'll now be able to determine which of these is best for you to use in your code.
The above is the detailed content of Compare PHP's isset(), empty() and is_null() functions. For more information, please follow other related articles on the PHP Chinese website!