View the alias in php from the source code

Source: Internet
Author: User
Today, a friend posted a question about the difference between exit and die on Baidu. There are two differences: die exits and releases the memory, and exit exits but does not release the memory. This explanation is clearly wrong. we have read the manual before and said that the two are just aliases. Today, a friend posted Baidu to know a question about the difference between exit and die.
The standard answer in the answer is:

There are two differences: die is to exit and release the memory, exit is to exit but not release the memory.

This explanation is obviously wrong. we have read the manual before and said that the two are only alias relationships, and they are all the same.
But I am still curious. I decided to look for clues from the source code to see how php handles this "Alias ".

First of all, it should be clear that die and exit are both language structures rather than functions. Many beginners cannot understand the differences between language structures and functions. in plain words, the language structure can be understood as an identifier of the syntax itself. Like +,-, *, and/are also language structures. if, else, for, and while are language structures. Is part of the syntax itself. Any language will have these things, because the computer does not think that ++ should do addition. This requires the compiler to convert the machine code to the instruction set that can be recognized by the cpu.

The entire process for php to execute the source code is: first, according to the definition in zend_language_scanner.l, convert the language structures such as echo and if in the source code into tokens such as T_ECHO and T_IF, in addition, spaces in the source code are removed to comment out the characters irrelevant to the program logic ., Some short expressions are formed, which is the lexical analysis stage. The tokens are then converted to op code according to the token defined in zend_vm_opcodes.h. Then execute the op code in one row.

The above describes the php compilation and execution processes and the definition of the language structure. Next, go to the topic.

We should also remember that php has many alias functions, such as implode and join. Both the alias function and the alias language structure are the same in terms of actual results, but the source code processing methods are certainly different.

Let's take a look at how the alias language structure is handled. let's look at the alias function later.

Zend_language_parser.c defines a macro
# Define T_EXIT 300

An enum is also defined, which also contains
Enum yytokentype {
...
T_EXIT = 300,
....
}

Here we can tell you that the token T_EXIT has a code of 300.


Let's look at zend_language_scanner.l, which contains several lines of code.

"Exit "{
Return T_EXIT;
}

"Die "{
Return T_EXIT;
}

Obviously, when php performs lexical analysis, the T_EXIT token will be returned regardless of exit or die. Here we can prove that the internal processing of die, exit, and php is exactly the same.

You can also use the following php code to determine:
Var_dump (token_get_all (" "));

In the returned result, the token code corresponding to die and exit is 300.


We can confirm the problem of die and exit. Here, another problem arises. It is also a detail that I have always ignored:
&, | Is it the same as and or?

I confess first, I thought the same thing before, and thought it was a pure alias relationship. However, after seeing the source code today, we found that it was completely different tokens. Take & AND for example:

Or zend_language_scanner.l

"&&"{
Return T_BOOLEAN_AND;
}

"AND "{
Return T_LOGICAL_AND;
}

One is Boolean and the other is logic and"

Different tokens are used. That must be different. I don't want to sell it out here. google can find many answers. In fact, the two most essential differences are their different priorities:

$ A = 1 & 0;
$ B = 1 AND 0;
Var_dump ($ );
Var_dump ($ B );

The former will calculate 1 & 0 First, get the result, and then assign it to $ a. The latter will first assign 1 to $ B. Therefore, the result is
Bool (false) int (1)

Now we should be clear about the details here. Note the following when using it.


What I just talked about is the "Alias" of the language structure. how can I deal with the function alias in php?
Example of implode and join:

In basic_function.c, you can find the following line:

PHP_FALIAS (join, implode, arginfo_implode)
Obviously, it is the role of the PHP_FAILIAS macro. We can also find many examples below. for example, ini_set and ini_alter are Alias relationships. If you want to go further, follow the macro.

Php. h
# Define PHP_FALIAS ZEND_FALIAS
PHP_FALIAS points to ZEND_FALIAS again.

Zend_API.h
# Define ZEND_FALIAS (name, alias, arg_info) ZEND_FENTRY (name, ZEND_FN (alias), arg_info, 0)
...
# Define ZEND_FENTRY (zend_name, name, arg_info, flags) {# zend_name, name, arg_info, (zend_uint) (sizeof (arg_info)/sizeof (struct _ zend_arg_info)-1 ), flags },

The next step is the function initialization and so on. we also know that alias functions also know what is going on.

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.