PHP isset and Is_null differences

Source: Internet
Author: User
Tags parse error

The function of isset and Is_null is almost exactly the same.

is isset the opposite alias of a is_null?

Eh, to say the difference, that is really a lot of ~

Everything is different because: Is_null is a function , Isset is a statement .

Isset is a statement, like Echo, print, which is a language structure of PHP itself.

And Is_null is a function, like our normal function, can be called a variable function.

You might say, well, well, I know the difference between a function and a statement, but what difference does it make?

Eh, the so-called statement, language structure, that is, the language itself to support the statement, identifier.

For example, for, foreach, continue, and so on, they are "erased" (logically replaced) at the moment of parsing.

Let's take a look at how isset this statement was "erased" in the parsing process.

1. First, in lexical analysis, Isset is recognized as a t_isset identifier. 2. In the parsing phase, the Isset ($var) is parsed into a opcode:zend_isset_isempty_vars.

You can understand that isset just want the macros in C to be started before compiling/executing.

Because of this, in performance, there will be the following differences:

Because Is_null is a function, it can be invoked in the following way:

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

<?phpIs_null(intval("0x45"));//okisset(intval("0x45"));//php Fatal error:can ' t use function return value in write contextIs_null(Null);//okisset(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:

<?php $a = "Laruence" : isset        ($a); //spents: 1.15165400505s Is_null ($a); //spents: 3.78501200676s ?>

Because Isset is called isset, it does not produce notice when it detects undefined variables:

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

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:

<?php$a="Laruence":isset($a);//spents: 1.15165400505sIs_null($a);//spents: 3.78501200676s$a===Null;//spents: 1.21655392647s?>

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.