Home Backend Development PHP Tutorial Differences between empty(), is_null(), and isset() functions in PHP_PHP Tutorial

Differences between empty(), is_null(), and isset() functions in PHP_PHP Tutorial

Jul 13, 2016 am 10:42 AM
empty isset null php function the difference us

empty(), is_null(), isset() truth table (difference)

Let’s first take a look at the functional descriptions of these three functions www.bKjia.c0m

isset determines whether the variable already exists. If the variable exists, it returns TRUE, otherwise it returns FALSE.

empty determines whether the variable is empty. If the variable is a non-empty or non-zero value, empty() returns FALSE. In other words, "", 0, "0", NULL, FALSE, array(), var $var; and objects without any attributes will be considered empty, and TRUE will be returned if the variable is empty.

is_null determines whether the variable is NULL

How about this? This explanation is generally used, but this explanation is already very confusing. Let’s analyze it with specific examples below!


From this we can find that as long as the variable is "" or 0, or false or null, empty will return true as long as these values ​​​​are.

isset only determines whether the variable exists. As long as your variable is not null or unassigned, the return result will be true. If you use isset() to test a variable that is set to NULL, it will return FALSE. Also note that a NULL byte ("") is not equivalent to PHP's NULL constant.

And is_null is exactly the inverse result of isset. We can think of it as !isset, which is an inverse operation of isset.

From the above examples, we can also draw the following conclusions (which will be often used in programming in the future):

Assume $var is any type

When empty($var) is true, (bool)($var) is false. vice versa.

When is_null($var) is true, isset($var) is false. vice versa.

For example:

$i=$j+1;

Here is_null($j) is true (it can be understood that isset($j) is false, because the variable $j is not declared in advance)

Two other points to note are:

(1) empty() only detects variables, and detecting anything that is not a variable will result in a parsing error. In other words, the following statement will not work: empty(addslashes($name)).

(2) isset() can only be used for variables, because passing any other parameters will cause a parsing error. If you want to check whether a constant has been set, use the defined() function.


Summary and summary of the differences between isset, empty, is_null

What I just introduced: checking variables and parameter types is the basis of the differences between these three functions, and it is also the most easily overlooked. I saw many articles comparing these three functions on the Internet. These are rarely covered. What I want to talk about next is the difference when both check existing variables.

Copy the code as follows
$a=100;
$b="";
$c=null;
//isset check
echo "isset","$a=$a",isset($a)?"define":"www.111cN.net undefine","rn";
echo "isset","$b=$b" ,isset($b)?"define":"undefine","rn";
echo "isset","$c=$c",isset($c)?"define":"undefine"," rn";
unset($b);
echo "isset","$b",isset($b)?"define":"undefine","rn";
$b=0 ;
echo "rnrn";

//empty check
echo "empty","$a=$a",!empty($a)?"no empty":"empty" ,"rn";
echo "empty","$b=$b",!empty($b)?"no empty":"empty","rn";
echo "empty"," $c=$c",!empty($c)?"no empty":"empty","rn";
unset($b);
echo "empty","$b",! empty($b)?"no empty":"empty","rn";
$b=0;
echo "rnrn";

//is_null check
echo " is_null","$a=$a",!is_null($a)?"no null":"null","rn";
echo "is_null","$b=$b",!is_null( $b)?"no null":"null","rn";
echo "is_null","$c=$c",!is_null($c)?"no null":"null"," rn";
unset($b);
echo "is_null","$b",is_null($b)?"no null":"null","rn";



Through the above simple test, we can generally know that when a variable exists: isset, empty, is_null detection, and get the value. There are more variables not exemplified above. In fact, the test found:

empty

If variable is a non-empty or non-zero value, empty() returns FALSE. In other words, "", 0, "0", NULL, FALSE, array(), var $var, undefined; and objects without any properties will be considered empty, and TRUE will be returned if var is empty. .

isset

Returns TRUE if the variable exists (not NULL), otherwise returns FALSE (including undefined). The variable value is set to: null, and the return value is also false; after unsetting a variable, the variable is canceled. Note that isset handles NULL value variables specially.

is_null

Check whether the incoming value [value, variable, expression] is null. Only if one variable is defined and its value is null will it return TRUE. Others will return FALSE [An error will occur after undefined variables are passed in. ! 】.

For more details, please check: http://www.bKjia.c0m/phper/php/56681.htm

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/667909.htmlTechArticleempty(), is_null(), isset() truth table (difference) Let’s take a look at these 3 first Function description of this function www.111cn.net isset determines whether the variable already exists. If the variable exists, it returns TRUE, otherwise...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

What are the basic requirements for c language functions What are the basic requirements for c language functions Apr 03, 2025 pm 10:06 PM

C language functions are the basis for code modularization and program building. They consist of declarations (function headers) and definitions (function bodies). C language uses values ​​to pass parameters by default, but external variables can also be modified using address pass. Functions can have or have no return value, and the return value type must be consistent with the declaration. Function naming should be clear and easy to understand, using camel or underscore nomenclature. Follow the single responsibility principle and keep the function simplicity to improve maintainability and readability.

Explain the match expression (PHP 8 ) and how it differs from switch. Explain the match expression (PHP 8 ) and how it differs from switch. Apr 06, 2025 am 12:03 AM

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

What are the differences and connections between c and c#? What are the differences and connections between c and c#? Apr 03, 2025 pm 10:36 PM

Although C and C# have similarities, they are completely different: C is a process-oriented, manual memory management, and platform-dependent language used for system programming; C# is an object-oriented, garbage collection, and platform-independent language used for desktop, web application and game development.

How to use XPath to search from a specified DOM node in JavaScript? How to use XPath to search from a specified DOM node in JavaScript? Apr 04, 2025 pm 11:15 PM

Detailed explanation of XPath search method under DOM nodes In JavaScript, we often need to find specific nodes from the DOM tree based on XPath expressions. If you need to...

What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? Apr 07, 2025 am 12:02 AM

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

Why do you need to call Vue.use(VueRouter) in the index.js file under the router folder? Why do you need to call Vue.use(VueRouter) in the index.js file under the router folder? Apr 05, 2025 pm 01:03 PM

The necessity of registering VueRouter in the index.js file under the router folder When developing Vue applications, you often encounter problems with routing configuration. Special...

Describe the purpose and usage of the ... (splat) operator in PHP function arguments and array unpacking. Describe the purpose and usage of the ... (splat) operator in PHP function arguments and array unpacking. Apr 06, 2025 am 12:07 AM

The... (splat) operator in PHP is used to unpack function parameters and arrays, improving code simplicity and efficiency. 1) Function parameter unpacking: Pass the array element as a parameter to the function. 2) Array unpacking: Unpack an array into another array or as a function parameter.

See all articles