What is the difference between isset and is_null?
Looking at the manual, the functions of isset and is_null are almost completely "opposite"...
Is it isset? Is it the opposite alias of is_null?
Hey, there are really many differences~
All the differences are because: is_null is a function and isset is a statement.
isset is a statement, like echo and print, it is a language structure of PHP itself.
And is_null is a function, like our ordinary functions, it can be called as a variable function.
You Maybe you will say, okay, okay, I know the difference between functions and statements, but what is the difference between TMD?
Hey, the so-called statements and language structures are, in other words, statements and identifiers supported by the language itself.
For example, for, foreach, continue, etc., they are "erased" (logically replaced) at the moment of syntax analysis.
Let us take a look at the isset statement during the syntax analysis process. How to be “erased”.
1. First, during lexical analysis, isset will be recognized as the T_ISSET identifier.
2. During the syntax analysis stage, the isset($var) instruction will be analyzed into an Opcode: ZEND_ISSET_ISEMPTY_VARS.
You can understand that isset is like a macro in C language, which has been expanded before compilation/execution.
Because of this, there will be the following difference in performance:
Because is_null is a function, So it can be called as follows:
Copy code The code is as follows:
$var = NULL;
$func = "is_null";
$func($var);
?>
However, because isset is a statement, it cannot be called like this.
Because is_null is a function, it can accept function return values as parameters, but isset cannot (of course, if PHP wants to support it, it is actually possible, but it will increase the complexity of the compilation phase):
Copy code The code is as follows:
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 talked about so many shortcomings of isset, let’s talk about its advantages:
Because isset is a statement, it is fast!
In a simple test statement loop of 10 million times, the comparison results are as follows:
Copy code Code As follows:
$a="laruence":
isset($a); //Time: 1.15s
is_null($a); / /Time: 3.89s
?>
Because isset is called isset, so when it detects undefined variables, it will not generate NOTICE:
Copy code The code is as follows:
isset($laruence);
//OK
is_null($laruence);
//PHP Notice: Undefined variable: laruence
?>
So, what suggestions do I have for when to use isset and when to use is_null?
Hey, My suggestion is to use functions to do what functions should do ~ Does it sound like nonsense?
isset => is set? => Is the variable assigned (declared)?
is_null => is null ? => Is the variable NULL?
In addition, if you want to use is_null, I suggest using "=== NULL" instead. Not only does it have the same semantics as is_null, the results are the same, and the speed is almost the same as isset:
In a loop of 10 million simple detection statements, the comparison results are as follows:
Copy the code The code is as follows:
< ?php
$a="laruence":
isset($a); //Time taken: 1.15s
is_null($a); //Time taken: 3.88s
$a=== NULL; //Time taken: 1.22s
?>
http://www.bkjia.com/PHPjc/328125.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/328125.htmlTechArticleWhat is the difference between isset and is_null? According to the manual, the functions of isset and is_null are almost completely "opposite and the same" .. Is isset just the opposite alias of is_null? Hey, if you want to talk about the difference,...