But there are times when you find that you don't achieve what you want in PHP.
I do not discuss the specific reasons, PHP practical applications are generally used isset () function to make judgments.
Examples are as follows:
The code is as follows |
Copy Code |
if (isset ($_get[' name '))//Determines whether the value is empty. { echo "value is null"; } |
Example
The code is as follows |
Copy Code |
<?php Predefined variable array: null character, character 0, array 0,null, Boolean false, empty array $arr _var = Array (', ' 0 ', 0, NULL, FALSE, Array ()); foreach ($arr _var as $value) { Echo ' <br> incoming value is: '. $value. ' <br> '; if (!isset ($value)) { Echo ' Isset () ==false<br> '; } if (empty ($value)) { Echo ' Empty () ==true<br> '; } if (! $value) { Echo ' self==false<br> '; } if (Is_null ($value)) { Echo ' Is_null () ==true<br> '; } } ?> Output results: The incoming values are: Empty () ==true Self==false Incoming values are: 0 Empty () ==true Self==false Incoming values are: 0 Empty () ==true Self==false The incoming values are: Isset () ==false Empty () ==true Self==false Is_null () ==true The incoming values are: Empty () ==true Self==false Incoming values are: Array Empty () ==true Self==false |
Summary one: As can be seen from the results, "", 0, "0", NULL, FALSE, Array (), empty () and the "variable itself as arguments" result in the same, are treated as "null", and Isset () and Is_null () only when NULL, as "null "To deal with (note that false is not considered null)
comparison of Isset and Is_null
Is_null is a function, so it can be invoked in the following way:
The code is as follows |
Copy Code |
<?php $var = NULL; $func = "Is_null"; $func ($var); ?>
|
And, isset because it is a statement, so it cannot be called.
Because Is_null is a function, it can accept the function return value as the parameter, and Isset not (of course, if PHP wants to support, it is also possible, but it will increase the complexity of the compilation phase):
The code is as follows |
Copy Code |
<?php Is_null (Intval ("0x45")); Ok Isset (Intval ("0x45")); PHP Fatal error:can ' t use function return value in write context Is_null (NULL); Ok Isset (NULL); PHP Parse error:syntax Error ?>
|
Having said so many isset flaws, say something about its advantages:
Because Isset is a statement, so it's fast!
In a 10 million-time simple detection statement loop, the results are as follows:
The code is as follows |
Copy Code |
<?php $a = "Laruence": Isset ($a); Spents: 1.15s Is_null ($a); Spents: 3.89s ?>
|
Because Isset is called isset, it does not produce notice when it detects undefined variables:
The code is as follows |
Copy Code |
<?php Isset ($laruence); Ok Is_null ($laruence); PHP notice:undefined variable:laruence ?>
|
So, what do I suggest when I use Is_null with isset?
Eh, my advice is to use functions to do what the function should do ~, sounds like nonsense?
Isset => is set? => variable has been assigned a value (declaration)
Is_null => is null? is the => variable null?
In addition, if you want to use Is_null, I recommend using "= = NULL" Instead, it is not only semantic and is_null consistent, the result is consistent, the speed and isset similar:
In a 10 million-time simple detection statement loop, the results are as follows:
The code is as follows |
Copy Code |
<?php $a = "Laruence": Isset ($a); Spents: 1.15s Is_null ($a); Spents: 3.88s $a ===null; Spents: 1.22s ?> |
Instructions for use:
1, null value, 0, False assignment results are isset to TRUE
2, NULL will be isset to FALSE
3, Isset () also applies to array elements and object elements of the check. If an array or object instance is not defined, the instrumented array element/object element will be returned false.
Report:
PHP's Isset () function
Format: bool Isset (mixed var [, mixed Var [, ...]])
Function: Detect whether the variable is set
return value:
Returns FALSE if the variable does not exist
Returns FALSE if the variable exists and its value is null
Returns ture if the variable exists and the value is not NULL
When you check multiple variables at the same time, each item returns TRUE when it meets the previous requirement, otherwise the result is FALSE
Version: PHP 3, PHP 4, PHP 5
More Description:
After you use unset () to free a variable, it is no longer a isset ().
Isset () can only be used for variables, and passing any other parameter will result in a parse error.
Detects whether a constant is set to use the defined () function.