About "aliases" in PHP
Today, a friend posted Baidu know a question to ask exit and die difference.
The answer in the inside probably means:
Two have a difference, die is to exit and release memory, exit is to exit but do not release memory.
This explanation is obviously wrong, and we've all read the manual that the two are just aliases, except that they're all the same.
However, I am still very curious, decided to find clues from the source code to see how PHP handled the "alias".
The first thing to be clear is that both die and exit are language constructs rather than functions. Many beginners do not understand the language structure and function of the difference, with the popular point, the language structure can be understood as a symbol of the grammar itself. Like + 、-、 *,/These are also language constructs, if, else, for, and while, these are language constructs. is part of the grammar itself. Any language will have these things, because the computer sees + does not think that should do the addition. This requires the compiler to convert to machine code, which is the set of instructions that the CPU can recognize.
PHP executes the source of the entire process for, first, according to ZEND_LANGUAGE_SCANNER.L definition, the source of the ECHO, if and other language structure into similar t_echo, t_if these tokens, and will remove the source of space, Note these characters that are not related to program logic. , some short expressions are formed, which is the lexical analysis phase. These tokens are then converted to OP code, as defined in Zend_vm_opcodes.h. Then one line executes these op code.
The above probably explains the process of compiling and executing PHP, as well as the definition of the language structure. Let's get down to the chase.
We should also remember that there are many alias functions in PHP, such as: implode and join. Either the alias function or the alias language structure, from the actual effect point of view, are the same, but the source of the processing method must be different.
Let's take a look at how this alias language structure is handled, and then look at the alias function later.
In Zend_language_parser.c, a macro is defined
#define T_EXIT 300
It also defines an enum, which also has
Enum Yytokentype {
...
T_exit = 300,
....
}
This tells us that t_exit this token, whose code is 300.
Look again at ZEND_LANGUAGE_SCANNER.L, which has so few lines of code.
"Exit" {
return t_exit;
}
"Die" {
return t_exit;
}
Obviously, PHP does lexical analysis, regardless of the exit or die, will return t_exit this token. From here the wine can prove that die and exit, and then PHP internal processing is exactly the same.
You can also use the following PHP code to determine:
Var_dump (Token_get_all (" "));
The token code for Die and exit in the returned result is 300.
We can already confirm the problem of die and exit. Here, one more question. is one of the details I've been ignoring:
&&, | | Is it the same as and, or?
I confess first, I always thought the same, thought is purely the alias relationship. But after seeing the source code today, we found that it was completely different tokens. Take && and and for example:
or ZEND_LANGUAGE_SCANNER.L?
"&&" {
return t_boolean_and;
}
"and" {
return t_logical_and;
}
One called boolean "with", one called "logical" and "
Different tokens are used. That must be different. Here I also do not suspense, Google can find a lot of answers, in fact, the two most substantial difference is the priority difference:
$a = 1 && 0;
$b = 1 and 0;
Var_dump ($a);
Var_dump ($b);
The former tries to calculate 1 && 0 first, and then assigns a $ A to the result, which assigns 1 to the $b first, so the result is
BOOL (FALSE) int (1)
You should know the details here. When you use it, you need to pay attention.
What you just said is the "alias" of the language structure, so how does the function alias in PHP work?
Take implode and join examples:
BASIC_FUNCTION.C, you can find the following line:
Php_falias (Join,implode,arginfo_implode)
So obviously, it is the php_failias of this macro. There are many more, such as Ini_set and Ini_alter, which are also alias relationships. If you want to dig deeper, go after the macro.
In Php.h
#define Php_faliaszend_falias
Found Php_falias and pointed to Zend_falias.
In 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},
Further down is the function initialization and so on, we also know that the alias function also know what is going on.
------Solution--------------------
Study, the general habit with exit;
------Solution--------------------
------Solution--------------------
I've seen it.
------Solution--------------------
Learn
Watch a joke on the app today
Also speaking of PHP exit with die
Here's an example of having a baby ..................
------Solution--------------------
First, the problem of aliases has long been seen.
In addition && and I only know that when the condition 1 && condition 2 is used, if the condition 1 is false, then the condition 2 will not be executed, and the 2 will be calculated, today is to know the essence of the reason.
------Solution--------------------
Learn ...
------Solution--------------------
References:
Citation: First, the problem of aliases has long been seen.
Also && And and I only know when conditions of use 1 && Condition 2, if the condition 1 is false, then the condition 2 will not be executed, and will calculate 2, today is to know the essential reason.
No
Whether it is && or and, when the left expression evaluates the result
... It seems that I have always misunderstood ah, Docheren brother's Patience explained.
------Solution--------------------
Xie Xie
Thank
------Solution--------------------
See Token_get_all () This function for the first time
------Solution--------------------
Learn knowledge
Here it comes
------Solution--------------------
Learn the ...
------Solution--------------------
Thank you so much for the landlord! Technical posts like this do not often see.
------Solution--------------------
I have been using die, one day I asked my classmates used no, he said he exit, I froze for a long time just remembered is the alias ...
------Solution--------------------
It's really good!!
------Solution--------------------
Technical posts,
Useful
------Solution--------------------
OX MAN!!!!!!
------Solution--------------------
Usually use is not many! Understanding is not very clear, such a look, Ah, the original is such!
------Solution--------------------
------Solution--------------------
These can be written on a blog.
------Solution--------------------
Study, help the top