Online search die and exit two functions, most of the "standard answer" is that die is to exit and release memory, exit is exited but does not release memory.
This explanation is clearly wrong, as the PHP manual has said, "Die-equivalent to exit." This language construct are equivalent to exit (). "The two are just aliases, and they are exactly the same.
But I'm still curious about the decision to look for clues from the source to see how PHP handles this "alias."
to be clear first, die and exit are "language construct" rather than functions, there are many online also said that a certain number is a function, so there is no return value is the structure, a lot of beginners always confused language structure and function of the difference, in layman's terms, The language structure can be understood as an identifier 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 executes the source code when the whole process, first of all, according to ZEND_LANGUAGE_SCANNER.L defined in 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. 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 business.
We should also remember that there are many alias functions in PHP, 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.
ZEND_LANGUAGE_PARSER.C defines a macro
#define T_EXIT
also defines an enum, which also has
The code is as follows |
Copy Code |
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.
The code is as follows |
Copy 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 the wine can be proved, die and exit, and then PHP internal processing is exactly the same.
You can also use the following PHP code to determine:
The code is as follows |
Copy Code |
<?php Var_dump (Token_get_all ("<?php die;exit;? > "));
|
The die and exit corresponding token code in the returned result are 300.
Now about die and exit issues, I think we should be able to determine, but the name is different, the effect is the same, there is no so-called unloading the memory problem.
PHP Manual: Die () equivalent to exit ().
Description: Die () and exit () are aborted script execution functions; in fact, exit and die these two names point to the same function, die () is an alias for the exit () function. The function accepts only one parameter, either a value returned by a program or a string, or a parameter without input, and the result has no return value.
Reference: Although the two are the same, there are also subtle selectivity in the use. For example:
When the value passed to the exit and die functions is 0 o'clock, it means that the execution of the script is terminated prematurely, usually with the name exit ().
The code is as follows |
Copy Code |
echo "1111"; Exit (0); echo "2222"; 22222 will not be output, because the program runs to exit (0), the script has been terminated prematurely, "immediately expired."
|
When an error occurs, you can pass a string to it, which is output to the system terminal, usually using the name Die ().
The code is as follows |
Copy Code |
$FP =fopen ("./readme.txt", "R") or Die ("Cannot open the file");
|
In this case, if the fopen function is invoked to return a Boolean value of false, die () terminates the script immediately and prints immediately
The string passed to it, "can say one or two words before death."