Compare PHP's 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.
-
isset()
: You can useisset()
to determine whether a variable has been declared and is different fromnull
. -
empty()
: Used to determine whether the variable exists and the value of the variable is not equal tofalse
. -
is_null()
: This function is used to check whether the variable isnull
.
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
.
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.
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. ?>
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.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.
