About "aliases" in PHP

Source: Internet
Author: User
Tags aliases exit in
PHP Alias language structure

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_falias Zend_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.


Reply to discussion (solution)

Study, the general habit with exit;

I've seen it.

Learn
Watch a joke on the app today
Also speaking of PHP exit with die
Here's an example of having a baby ..................

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.

Learn ...

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.

No

Either && or and, the right expression is no longer evaluated when the left expression evaluates to False

$i = 0;
1>2 && $i = 1;
echo $i;
-----------------
$i = 0;
1>2 and $i = 1;
echo $i;

Both of these codes result in 0. This example illustrates this problem and,&& is the same as and on the assertion issue.
Same
1<2 | | $i = 1; and 1<2 or $i = 1; the effect is the same here.


The difference between && and and is higher than the priority of,&& =, and the priority of and is lower than =, so:
$a = 1 && 0;
Can be understood as:
$a = (1 && 0);

and
$b = 1 and 0;
Can be understood as:
($b = 1) and 0;

Reference to the 5 floor None01 's reply: First, the problem of aliases long learned.
Also &amp;&amp; And and I only know when conditions of use 1 &amp;&amp; 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.

Xie Xie
Thank

See Token_get_all () This function for the first time

Learn knowledge
Here it comes

Learn the ...

In fact, we understand how PHP handles the alias problem, we can even customize the PHP syntax, we add the new language structure as an alias for other statements.


Only need to ZEND_LANGUAGE_SCANNER.L in the
"Exit" {
return t_exit;
}

"Die" {
return t_exit;
}

Below, add
"Stop" {
return t_exit;
}

Recompile, congratulations. The Stop statement can be used. Because it is only an alias for other language constructs, the returned token is still existing and does not add new ones, so no subsequent processing is required. The Stop statement has the same effect as exit, die, and is purely an alias relationship.

Of course, if you need to add a new feature to the PHP language structure, you also need to add new tokens and add processing logic to the new token. It's more than just a place to change.

Thank you so much for the landlord! Technical posts like this do not often see.

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

It's really good!!

Technical posts,
Useful

OX MAN!!!!!!

Usually use is not many! Understanding is not very clear, such a look, Ah, the original is such!


These can be written on a blog.

To learn, educated, thank you landlord spend time finishing.

Study the

  • Related Article

    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.