: This article mainly introduces the differences between die and exit in php. For more information about PHP tutorials, see. First think about a 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:
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.
// 22222 is not output because the script has been terminated before the exit (0) is run, and the script is "disconnected 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.
"Cannot open this file"
// In this case, if the fopen function is called and a Boolean value of false is returned, die () terminates the script immediately and prints the script immediately.
// The string that is passed to it. "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?
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 ".
That is to say, exit (123) only outputs a running state of 123, instead of actually outputting the string 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:
The above introduces the differences between die and exit in php, including the content, and hope to be helpful to friends who are interested in PHP tutorials.