Consider a question first:
What does the following code show to the page?
The code is as follows |
|
<?php Die (123); ?> |
There was a time when I thought the page would show 123, but the result told me that the answer was wrong and the page was blank!
Always do not know why, anyway not output 123, in order to let the page output 123, I modify it to the following code:
The code is as follows |
|
<?php echo ' 123 '; Die (); ?>
|
A piece of information on the network:
The difference between exit () and Die () in PHP
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 ().
The code is as follows |
|
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 ().
The code is as follows |
|
$FP =fopen ("./readme.txt", "R") or Die ("Cannot open the file"); |
2//In this case, if the fopen function is invoked to return a Boolean value false, Die () will immediately terminate the script and print immediately
3//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?
The code is as follows |
|
<?php Die (123); or exit (123); ?> |
Own summary:
1, function, die () is equivalent to exit ();
2, PHP has a variety of ways to run, can be the form of a Web site, but also script form (do not require a Web server).
When PHP is running as a script, it is recommended to use Exit ():
For example, Bash the Shell scripting language, when it stops running, terminates the script with the exit () function, and allows the output point content to be in the running environment (typically stored in a global variable), but the output can only be a number representing the "End state of the command".
In other words, exit (123) simply outputs a running state of 123 instead of actually outputting 123 to the console. If you want to output 123 to the console, the code must be changed to the following form:
The code is as follows |
|
<?php
Exit (' 123 '); ?> |
When PHP is running as a Web site, it is recommended to use Die ():
But at this point die (number), there is no meaning, because it will not output the number of strings 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
The code is as follows |
|
<?php Die (' 123 '); ?> |