About aliases in PHP

Source: Internet
Author: User
Tags aliases definition exit join php code

What's the difference between exit and die in PHP?

There are two differences, die is to exit and free memory, exit is exited but does not release memory.

This explanation is obviously wrong, and we've all read the manual before, saying that the two are just aliases, and that's exactly the same thing.

But I am still very curious, decided to look for clues from the source, to see how PHP is handled by this "alias."

The first thing to be clear is that both die and exit are language constructs rather than functions. Many beginners always confuse the language structure and function of the difference, in layman's terms, language structure can be understood as a sign of the grammar itself. Like + 、-、 *,/These are all language constructs, if, else, for, while, these are language constructs. is part of the grammar itself. Any language will have these things, because the computer sees + does not think it should be additive. This requires the compiler to convert to machine code, which is the set of instructions that the CPU can recognize.

PHP implementation of the source code when the whole process, first of all, according to the definition of ZEND_LANGUAGE_SCANNER.L, the source of the ECHO, if such as the language structure into similar t_echo, t_if these token, and will remove the space in the source code, Note these characters that are not logically related to the program. , the form of a few short expressions, this is the lexical analysis phase. These token are then converted to OP code as defined in Zend_vm_opcodes.h. Then one line executes these op code.

It probably explains the process of compiling and executing PHP, as well as the definition of language structure. Let's get down to business.

We should also remember that PHP has many alias functions, such as: implode and join. Whether the alias function or alias language structure, from the actual effect point of view, are the same, but the source of the processing method is certainly not the same.

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

Also defines an enum, which also has

Enum Yytokentype {

...

T_exit = 300,

....

}

This tells us that t_exit this token, its code is 300.

Then look at ZEND_LANGUAGE_SCANNER.L, which has so few lines of code.

<ST_IN_SCRIPTING> "Exit" {

return t_exit;

}

<ST_IN_SCRIPTING> "Die" {

return t_exit;

}

Obviously, PHP does lexical analysis, whether encountered exit or die, will return t_exit this token. From here it can be proved that die and exit, and then PHP internal processing is exactly the same.

You can also use the following PHP code to determine:

<?php

Var_dump (Token_get_all ("<?php die;exit;? > "));

The die and exit corresponding token code in the returned result are 300.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/PHP/

As for the die and exit issues, we can already determine. Here, a question is deduced. It's also one of the details I've been ignoring:

&&, | | With and, or?

I confess first, I always thought the same, thought is pure alias relationship. But today, after seeing the source, the discovery is completely different token. Take && and examples:

or the ZEND_LANGUAGE_SCANNER.L.

<ST_IN_SCRIPTING> "&&" {

return t_boolean_and;

}

<ST_IN_SCRIPTING> "and" {

return t_logical_and;

}

One called boolean "and", one called Logic "and"

The reason for using different token. That must be different. Here I also don't suspense, Google can find a lot of answers, in fact, the two most substantial difference is the priority of different:

$a = 1 && 0;

$b = 1 and 0;

Var_dump ($a);

Var_dump ($b);

The former tries to compute 1 && 0 First, then assigns the result to $a, which assigns 1 to $b;

BOOL (FALSE) int (1)

We should know the details here. You need to pay attention when you use it.

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's the role of PHP_FAILIAS this macro. There are also many, such as Ini_set and Ini_alter are alias relationships. If you want to delve 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},

And then down is the function initialization and so on, and we know that the alias function also knows what's 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.