In php, empty (), is_null (), and isset () functions can be used to determine whether a variable exists. in this case, empty (), is_null (), isset () where are the differences, such
In php, empty (), is_null (), and isset () functions can be used to determine whether a variable exists. in this case, empty (), is_null (), isset () what is the difference between them? if you don't understand it, let's take a look at the following.
Let's take a look at the functional descriptions of the three functions.
Isset determines whether a variable already exists. if the variable exists, TRUE is returned; otherwise, FALSE is returned.
Empty determines whether the variable is null. if the variable is not null or a 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, if the variable is null, TRUE is returned.
Is_null determines whether the variable is NULL
This is the general explanation, but it is confusing. let's take a look at the specific examples!
From this, we can find that if the variable is "" or 0, or false or null, empty returns true as long as these values are empty.
Isset only determines whether a variable exists. as long as the variable is not null or not assigned a value, the returned result is true. If you use isset () to test a variable that is set to NULL, FALSE is returned. Note that a NULL byte ("") is not equivalent to the NULL constant of PHP.
Is_null is exactly the opposite result of isset. we can regard it! Isset is an inverse operation of isset.
From the above examples, we can draw the following conclusions (which will be frequently used in programming in the future ):
Assume that $ var is of any type.
When empty ($ var) is true, (bool) ($ var) is false. And vice versa.
When is_null ($ var) is true, isset ($ var) is false. And vice versa.
Example: $ I = $ j + 1;
Here is_null ($ j) is true (it can be understood that because isset ($ j) is false, because $ j is not declared in advance)
Note the following two points:
(1) empty () only detects variables and any non-variables will cause parsing errors. In other words, the following statement does not work: empty (addslashes ($ name )).
(2) isset () can only be used for variables, because passing any other parameter will cause a parsing error. To check whether a constant has been set, use the defined () function.
Summary differences between isset, empty, and is_null
As described earlier: checking variables and parameter types are the basis for the differences between the three functions and are also the most overlooked. I can see many articles comparing these three functions on the Internet. This is rarely involved. Next I want to talk about the differences in checking all existing variables.
The code is as follows:
-
- $ A = 100;
- $ B = "";
- $ C = null;
- // Isset check
- Echo "isset", "$ a = $ a", isset ($ )? "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 = $ ",! Emptyempty ($ )? "No empty": "empty", "rn ";
- Echo "empty", "$ B = $ B ",! Emptyempty ($ B )? "No empty": "empty", "rn ";
- Echo "empty", "$ c = $ c ",! Emptyempty ($ c )? "No empty": "empty", "rn ";
- Unset ($ B );
- Echo "empty", "$ B ",! Emptyempty ($ B )? "No empty": "empty", "rn ";
- $ B = 0;
- Echo "rnrn ";
- // Is_null check
- Echo "is_null", "$ a = $ ",! Is_null ($ )? "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 know that when a variable exists: isset, empty, and is_null, the value is obtained. No more variables are provided. In fact, the test found that:
Empty
If the variable is not null or a non-zero value, empty () returns FALSE. In other words, "", 0, "0", NULL, FALSE, array (), var $ var, undefined, and objects without any attributes will be considered empty, if var is null, TRUE is returned.
Isset
If the variable exists (not NULL), TRUE is returned; otherwise, FALSE (including undefined) is returned ). If the value of a variable is set to null, false is returned. if a variable is unset, the variable is canceled. Note that isset has special processing for NULL variables.
Is_null
Checks whether the input value [value, variable, expression] is null. only if one variable is defined and its value is null, TRUE is returned. otherwise, FALSE is returned. [an error will occur after an undefined variable is passed in! ].