Example of the difference between PHP die () and exit () _php Example

Source: Internet
Author: User
Tags php code

Online search die and exit two functions of the difference, 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 am still very curious, decided to look for clues from the source, to see how PHP is handled by this "alias."

First of all, to be clear, die and exit are "language construct" rather than functions, online also have a lot of said that some of the return value is a function, So-and-so no return value is the structure, many beginners are 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 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 = +,

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
<ST_IN_SCRIPTING> "Die" {return

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:

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

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

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

In this case, if the fopen function is invoked to return the Boolean value false, Die () terminates the script immediately and prints the string passed to it, "one or two words before death".

Thank you for reading, I hope to help you, thank you for your support for this site!

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.