Examples of differences between PHP die () and exit ()

Source: Internet
Author: User
Tags exit in
PHP in the real difference between die () and exit () What is the reason, we Baidu found die is to exit and release memory, Exit is to quit but not to release memory, then really is this, the need for friends can refer to the next

Online search for the difference between die and exit two functions, most of the "standard answer" is that the die is to exit and release memory, exit is to exit but do 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, except for the exact same thing.

However, I am still very curious, decided to find clues from the source code to see how PHP handled the "alias".

First of all to be clear, die and exit are "language construct" rather than functions, online also have a lot of said that a return value is a function, such as no return value is the structure, a lot of beginners always confused language structure and function of the difference, with popular point words, The language structure can be understood as an identity 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 =,....}

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.

<ST_IN_SCRIPTING> "Exit" {return t_exit;} <ST_IN_SCRIPTING> "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:

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

The token code for Die and exit in the returned result is 300.

Now about die and exit question, I think we should be able to determine, but the name is different, the effect is the same, there is no so-called unloading memory problem.

PHP Manual: Die () equivalent to exit ().

Description: Both die () and exit () are abort script execution functions; the two names of Exit and die refer to the same function, and die () is the alias of 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 usually subtle selectivity in use. For example:
When passed to the exit and Die function, the value of 0 o'clock means that the execution of the script is terminated prematurely, usually with the name exit ().

echo "1111", exit (0), echo "2222",//22222 will not be output, because the script has been terminated prematurely, "expired immediately" when the program ran to exit (0).

When a program goes wrong, it can be passed a string, which is output on the system terminal, usually using the name Die ().

$FP =fopen ("./readme.txt", "R") or Die ("Cannot open the file");

In this case, if the fopen function is called to return a Boolean value of false, die () terminates the script immediately and prints the string passed to it, "you can say one or two words before death."

The above is the whole content of this article, I hope that everyone's study has helped.


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.