Difference between exit () and die () in PHP

Source: Internet
Author: User
Tags php session

Difference between exit () and die () in PHP

This article briefly analyzes the differences between exit and die commonly used in php. If you need them, you can take a look.

First, consider the following question:

What will the following code display to the page?


For a while, I always thought that the page would display 123, but the practice result tells me that the answer is wrong and the page is blank!

I don't know why. I don't want to output 123. To make the page output 123, I changed it to the following code:

<?phpecho '123';die();?>

A piece of online information:

Difference between exit () and die () in PHP

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 the Boolean value false is returned, die () the script will be terminated immediately, and the string that is printed // to it will be immediately printed. "You can say one or two more words before you die ".

Back to the previous topic, why does the following code not output 123 to the page?

<? Phpdie (123); // or exit (123);?>


1. In terms of function, die () is equivalent to exit ();

2. PHP can run in multiple ways, either in the form of a website or a script (no Web server is required ).

When PHP is run as a script, we recommend that you use exit ():

For example, the Bash Shell script language uses the exit () function to terminate the script when it is about to stop running, and allows the output of vertex content to the running environment (usually stored in a global variable), but the output content can only be numbers, indicating the "command end status ".

Reference: http://blog.snsgou.com/post-711.html

That is to say, exit (123) only outputs a running state of 123, rather than outputting 123 to the console. To output 123 to the console, the Code must be changed to the following format:


When PHP runs as a website, we recommend using die ():

However, die (number) is meaningless because it does not output a digital string to the page. That is to say, if you want to terminate the page and output a number, you have to change it to the following format:

Articles you may be interested in
  • Differences between echo, print, print_r, var_export, and var_dump in php
  • Differences and usage of return, exit, break, and contiue in PHP
  • Use break, continue, goto, return, and exit in multiple loops in PHP
  • Empty, is_null, and isset in php
  • Difference between execute and query methods in ThinkPHP
  • Differences among scrollHeight, scrollWidth, scrollLeft, and scrolltop in javascript
  • Differences between variables and functions in php after the static keyword is added
  • Differences between php session and cookie

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.