


Differences between empty(), is_null(), and isset() functions in PHP_PHP Tutorial
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

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

Created by Ripple, Ripple is used for cross-border payments, which are fast and low-cost and suitable for small transaction payments. After registering a wallet and exchange, purchase and storage can be made.

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

Discussing the hierarchical architecture in back-end development. In back-end development, hierarchical architecture is a common design pattern, usually including controller, service and dao three layers...

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

Laravel optimizes the web development process including: 1. Use the routing system to manage the URL structure; 2. Use the Blade template engine to simplify view development; 3. Handle time-consuming tasks through queues; 4. Use EloquentORM to simplify database operations; 5. Follow best practices to improve code quality and maintainability.

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.
