PHP empty,isset,is_null judgment Comparison (differences and similarities) Font: [Increase decrease] Type: Reprint do PHP development time, presumably in use: Empty,isset,is_null These several functions, encountered some problems. Even give your own program to bring some security problems of the bug. Most of the time, the Isset,empty are considered similar. Therefore, the development time, did not notice, a period as the process of judgment, there is a bug problem.
I. Examples and explanations
A. How do we judge a variable without a definition?
Copy CodeThe code is as follows:
<?php
#不存在 $test variables
$isset = Isset ($test)? " Test is define! ":" Test is undefine! ";
echo "Isset: $isset \ r \ n";
$empty =!empty ($test)? " Test is define! ":" Test is undefine! ";
echo "EMPTY: $empty \ r \ n";
$is _null=is_null ($test)? " Test is define! ":" Test is undefine! ";
echo "Is_null: $is _null\r\n";
The test results are:
The result is:Empty,isset first checks whether the variable exists and then detects the value of the variable. And Is_null just checks the value of the variable directly, whether it is null, so if the variable is undefined, an error will occur!
B, look at the respective received parameters are what?
isset function Parameters:
<?php
$test = 100;
echo isset ($test), isset (+), $isset ($b =100);
<br/>
<b>parse error</b>: Parse error, unexpected T_lnumber, expecting t_string or t_variable or ' $ ' in <b>ph pdocument3</b> on line <b>3</b><br/>
Empty function Parameters:
<?php
$test = 100;
echo Empty ($test), empty (+), empty ($b =100);
<br/>
<b>parse error</b>: Parse error, unexpected T_lnumber, expecting t_string or t_variable or ' $ ' in <b>ph pdocument3</b> on line <b>3</b><br/>
is_null function Parameters:
<?php
$test = 100;
echo Is_null ($test), is_null (+), Is_null ($b =100);
Run Result: no error.
The comparison results come out: theempty,isset input parameter must be a variable (the PHP variable starts with a $ character), and the Is_null input parameter can be as long as it is able to have a return value. (constants, variables, expressions, etc.). In the PHP manual, the parsing for them is that Empty,isset is a language structure and not a function, so it cannot be called by a variable function.
Two, summarize isset,empty,is_null difference:
Just introduced: check variables, and parameter types, this is the 3 functions of the basis of the differences, but also the most easily overlooked. See there are many articles on the Web that compare this 3 function. This is rarely involved. What I'm going to say is that the differences are in the case of checking for existing variables.
Instance:
Copy CodeThe code is as follows:
<?php
$a = 100;
$b = "";
$c =null;
Isset Check
echo "Isset", "\ $a = $a", isset ($a)? " Define ":" Undefine "," \ r \ n ";
echo "Isset", "\ $b = $b", Isset ($b)? " Define ":" Undefine "," \ r \ n ";
echo "Isset", "\ $c = $c", Isset ($c)? " Define ":" Undefine "," \ r \ n ";
Unset ($b);
echo "Isset", "\ $b", Isset ($b)? " Define ":" Undefine "," \ r \ n ";
$b = 0;
echo "\r\n\r\n";
Empty check
echo "Empty", "\ $a = $a",!empty ($a)? " No empty ":" Empty "," \ r \ n ";
echo "Empty", "\ $b = $b",!empty ($b)? " No empty ":" Empty "," \ r \ n ";
echo "Empty", "\ $c = $c",!empty ($c)? " No empty ":" Empty "," \ r \ n ";
Unset ($b);
echo "Empty", "\ $b",!empty ($b)? " No empty ":" Empty "," \ r \ n ";
$b = 0;
echo "\r\n\r\n";
Is_null Check
echo "Is_null", "\ $a = $a",!is_null ($a)? " No null ":" null "," \ r \ n ";
echo "Is_null", "\ $b = $b",!is_null ($b)? " No null ":" null "," \ r \ n ";
echo "Is_null", "\ $c = $c",!is_null ($c)? " No null ":" null "," \ r \ n ";
Unset ($b);
echo "Is_null", "\ $b", Is_null ($b)? " No null ":" null "," \ r \ n ";
Through this simple test, we can generally know that when a variable exists: isset,empty,is_null detection, the value of the situation is obtained. There are no examples of more variables. In fact, the test found:
Empty
If the variable is a non-null or nonzero value, empty () returns FALSE. In other words, "", 0, "0", NULL, FALSE, Array (), Var $var, undefined; and objects that do not have any properties will be considered empty and return TRUE if Var is empty.
Isset
Returns TRUE if the variable exists (not null), otherwise FALSE (including undefined). The variable value is set to: null, and the variable is canceled after the return is also false;unset a variable. Note that isset for null value variables, special handling.
Is_null
Detects if the passed-in value "value, variable, expression" is null, only one variable is defined, and its value is NULL, it returns TRUE. All others return FALSE "an undefined variable is passed in with an error!" 】.
Question: How do you tell if a variable is set and the value is NULL?
By comparison above, it is estimated that everyone, like me, will have this problem in mind. Detecting whether a variable is null can be used: Is_null, but an error occurs if the variable is undefined with it. Therefore, we think that the detection variable definition can be used: isset, but if a variable value is: null, it will return false. Haha, how to solve this problem? Waiting for everyone to share ...
Check that the variable exists and that the value is null.
Copy CodeThe code is as follows:
<?php
function Checknull ($a)
{
if (array_key_exists ($a, $GLOBALS))
{
global $ $a;
if (Is_null ($ $a))
return true;
}
return false;
}
$test =null;
Var_dump (Checknull ("Test"));
Var_dump (Checknull ("test1"));
Source: >
From for notes (Wiz)
PHP empty,isset,is_null judgment Comparison (differences and similarities)