Example of differences between php die () and exit (), dieexit

Source: Internet
Author: User

Example of differences between php die () and exit (), dieexit

The difference between the two functions: die and exit. Most of the "standard answer" is that die exits and releases the memory, while exit exits but does not release the memory.

This explanation is obviously wrong. The PHP Manual already says "die-Equivalent to exit (). this language construct is equivalent to exit (). the two are just alias relationships, and they are all the same.

But I am still curious. I decided to look for clues from the source code to see how php handles this "alias ".

First of all, it should be clear that both die and exit are "language construct" rather than functions. On the internet, there are also many saying that a certain person has a return value as a function, and a certain person has a structure without a return value, many beginners cannot understand the differences between language structures and functions. In plain words, the language structure can be understood as an identifier of the syntax itself. Like +,-, *, And/are also language structures. if, else, for, and while are language structures. Is part of the syntax itself. Any language will have these things, because the computer does not think that ++ should do addition. This requires the compiler to convert the machine code to the instruction set that can be recognized by the cpu.

The entire process for php to execute the source code is: first, according to the definition in zend_language_scanner.l, convert the language structures such as echo and if in the source code into tokens such as T_ECHO and T_IF, in addition, spaces in the source code are removed to comment out the characters irrelevant to the program logic ., Some short expressions are formed, which is the lexical analysis stage. The tokens are then converted to op code according to the token defined in zend_vm_opcodes.h. Then execute the op code in one row.

The above describes the php compilation and execution processes and the definition of the language structure. Next, go to the topic.

We should also remember that php has many alias functions, such as implode and join. Both the alias function and the alias language structure are the same in terms of actual results, but the source code processing methods are certainly different.

Let's take a look at how the alias language structure is handled. Let's look at the alias function later.

Zend_language_parser.c defines a macro

#define T_EXIT 300

An enum is also defined, which also contains

enum yytokentype {…T_EXIT = 300,….}

Here we can tell you that the token T_EXIT has a code of 300.

Let's look at zend_language_scanner.l, which contains several lines of code.

<ST_IN_SCRIPTING>”exit” {return T_EXIT;}<ST_IN_SCRIPTING>”die” {return T_EXIT;}

Obviously, when php performs lexical analysis, the T_EXIT token will be returned regardless of exit or die. Here we can prove that the internal processing of die, exit, and php is exactly the same.

You can also use the following php code to determine:

<?phpvar_dump(token_get_all(“<?php die;exit;?>”));

In the returned result, the token code corresponding to die and exit is 300.

Now, I think you can confirm the problem about die and exit, but the names are different and the effects are the same. There is no such problem as uninstalling or uninstalling the memory.

PHP Manual: die () Equivalent to exit ().

Note: Both die () and exit () Stop the script execution function. In fact, the names exit and die point to the same function, and die () is the alias of the exit () function. This function only accepts one parameter. It can be a value or a string returned by a program, or it can be left blank. The result does not return a value.

Reference: although the two are the same, they are also slightly selective. For example:
When the value passed to the exit and die functions is 0, it means that the execution of the script is terminated in advance. Usually exit () is used.

Echo "1111"; exit (0); echo "2222"; // 22222 is not output because the script has been terminated early when the program runs to exit (0, "Get angry immediately ".

When a program encounters an error, you can pass it a string, which is output on the system terminal as is. The name of die () is usually used.

$ Fp = fopen ("./readme.txt", "r") or die ("cannot open this file ");

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

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

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.