Differences between parsing isset and is_null

Source: Internet
Author: User
Tags parse error
Isset is a statement. like echo and print, isset is a language structure of PHP, while is_null is a function. it can be called as a variable function, just like a common function. What is the difference between isset and is_null,
In the manual, isset and is_null have almost identical functions "..
Is isset the opposite alias of is_null?
Well, there are many differences ~

Everything is different because is_null is a function and isset is a statement.
Isset is a statement. like echo and print, isset is a language structure of PHP itself.
Is_null is a function, which can be called as a variable function just like ordinary functions.
You may say, well, well, I know the difference between functions and statements, but what is the difference between TMD?
The so-called statement and language structure are the statements and identifiers supported by the language itself.
For example, for, foreach, and continue, they are "erased" at the moment of syntax analysis (logically replaced.
Let's take a look at how the isset statement is erased during syntax analysis.

1. first, during lexical analysis, isset will be recognized as T_ISSET identifier.
2. in the syntax analysis phase, the isset ($ var) command will be analyzed into an Opcode: ZEND_ISSET_ISEMPTY_VARS.

You can understand isset and think about the macros in the C language, which have been expanded before compilation/execution.
Because of this, there will be the following differences in performance:
Because is_null is a function, it can be called as follows:

The code is as follows:


$ Var = NULL;
$ Func = "is_null ";
$ Func ($ var );
?>


Because isset is a statement, it cannot be called like this.
Because is_null is a function, it can accept the return value of the function as a parameter, while isset is not (of course, if PHP wants to support it, it is also possible, but it will increase the complexity of the compilation phase ):

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
?>


After talking about the shortcomings of so many issets, let's talk about its advantages:
Because isset is a statement, it is fast!
In the 10 million simple check statement loop, the comparison results are as follows:

The code is as follows:


$ A = "laruence ":
Isset ($ a); // Time: 1.15 s
Is_null ($ a); // Time: 3.89 s
?>


Because isset is called isset, it does not produce NOTICE when detecting undefined variables:

The code is as follows:


Isset ($ laruence );
// OK
Is_null ($ laruence );
// PHP Notice: Undefined variable: laruence
?>


So, when to use isset and when to use is_null, what suggestions do I have?
Hi, my advice is to use functions to do what functions should do ~, It sounds like nonsense?
Isset => is set? => Is the variable assigned a value (declaration)
Is_null => is null? => Is the variable NULL?
In addition, if is_null is used, we recommend that you use "= NULL" instead. it not only has the same semantics and is_null, but also has the same results and speed as isset:
In the 10 million simple check statement loop, the comparison results are as follows:

The code is as follows:


$ A = "laruence ":
Isset ($ a); // Time: 1.15 s
Is_null ($ a); // Time: 3.88 s
$ A = NULL; // Time: 1.22 s
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.