Home > Backend Development > PHP Tutorial > Compare PHP's isset(), empty() and is_null() functions

Compare PHP's isset(), empty() and is_null() functions

王林
Release: 2023-09-04 20:26:02
Original
855 people have browsed it

Compare PHPs isset(), empty() and is_null() functions

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.

definition

Let's start with some definitions.

  1. isset(): You can use isset() to determine whether a variable has been declared and is different from null.
  2. empty(): Used to determine whether the variable exists and the value of the variable is not equal to false.
  3. is_null(): This function is used to check whether the variable is null.

PHP 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.

One similarity between

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

?>
Copy after login

Please note that empty() can be written using the isset() function:

<?php

function my_empty($x) {
  return !isset($x) || $x == false
}

?>
Copy after login

Of course, it's usually easier to use the built-in empty() function.

PHP 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.

?>
Copy after login

PHP 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.
Copy after login

IMPORTANT POINTS TO REMEMBER

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.

Final Thoughts

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template