In php, we have many methods to determine whether variables are null. php provides functions such as empty, isset, varnull, and is_null for variable judgment, next I will share with you how to use these methods. empty (... in php, we have many methods to determine whether a variable is null. php provides functions such as empty, isset, var = null, is_null, and so on for variable judgment, next I will share with you how to use these methods. A simple comparison between empty () and isset.
The instance code is as follows:
The above empty and isset objects that are judged to have no attributes will be considered null. var = null
Function: determines whether the variable is "null"
Note: variables and empty arrays whose values are 0, false, or empty strings "" or null will all be considered null.
Note: a significant difference from empty is that when the variable is not initialized, var = null will report an error.
The instance code is as follows:
$ A = 0; $ B = array (); if ($ a = null) echo '$ a is blank '. ""; if ($ B = null) echo '$ B is blank '. ""; if ($ c = null) echo '$ B is blank '. ""; // The displayed result is // $ a is blank // $ B is blank // Undefined variable: c
The is_null instance code is as follows:
$ A = null; $ B = false; if (is_null ($ a) echo '$ a is Null '. ""; if (is_null ($ B) echo '$ B is Null '. ""; if (is_null ($ c) echo '$ c is Null '. ""; // The displayed result is // $ a is NULL // Undefined variable: c
Variable = 0 or variable = 0
The instance code is as follows:
$ A = 0 $ a = "" $ a = 0 equals $ a = ""
When passing parameters in a php url, when the URL tail parameter of the php page shows id = 0 (for example, test. php? Id = 0), try to compare:
The instance code is as follows:
If (emptyempty ($ id) $ id = 1; // if id = 0, the id is also 1 if (! Isset ($ id) $ id = 1; // If id = 0, id will not be 1
Run the following code separately to detect the above inference:
If (emptyempty ($ id) $ id = 1; print $ id; // Get 1 if (! Isset ($ id) $ id = 1; print $ id; // get 0
Summary
In php, NULL and NULL are two different concepts, isset is mainly used to determine whether a variable has been initialized. empty can set TRUE to all variables whose values are "false", "empty", "0", "NULL", and "uninitialized ".
Is_null only determines variables with a value of "NULL" as TRUEvar = null and variables with a value of "false", "NULL", "0", and "NULL" as TRUEvar. === null only determines the variable with the value of "NULL" as TRUE.
Other functions that may use php variable judgment
Isset ($ var): // whether the variable has been declared empty ($ var): // whether the variable is null defined ($ var): // whether the constant has been defined define () is_array ($ var): // checks whether the measurement variable is an array is_null ($ var): // checks whether the measurement variable is null.
Address:
Reprinted at will, but please attach the article address :-)