Think of a problem first:
What does the following code show to the page?
<? Phpdie (123);? >
There was a time I always thought the page will show 123, but the practice results tell me that the answer is wrong, the page is blank!
I don't know why, anyway. Output 123, in order to let the page output 123, I changed it to the following code:
<? '123';d ie ();? >
A section of the online information:
The difference between exit () and Die () in PHP
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 a value of 0 is passed to the exit and die functions, it means terminating the execution of the script prematurely, usually with the name exit ().
" 1111 " ; exit (0"2222"; // 22222 is not 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 () will immediately terminate the script and print immediately // The string passed to it, "can say one or two words before death."
Back to the previous topic, why does the following code not output 123 to the page?
<? Phpdie (123); // or exit (123);? >
Self-Summary:
1, function, die () is equivalent to exit ();
2, PHP has a variety of modes of operation, can be the form of Web site, but also script form (do not need a Web server).
- When PHP is running as a script, exit () is recommended:
For example, the Bash Shell scripting language, when it stops running, terminates the script with the exit () function and allows the output point content to run (typically stored in a global variable), but the output can only be numeric , indicating "the end state of the command" ”。
Related Reference Links: http://blog.snsgou.com/post-711.html
That is, exit (123) simply outputs a run state of 123 instead of actually outputting the string 123 to the console . If you want to output 123 to the console , the code is changed to the following form:
<? phpexit ('123');? >
- When PHP is running as a Web site, it is recommended to use Die ():
But at this point Die (number), it makes no sense, because it does not output a numeric string to the page, that is, if you want to let the page terminate and output the number, you have to change to the following form
<? Phpdie ('123');? >
The difference between exit () and Die () in PHP