The difference between exit () and Die () in PHP

Source: Internet
Author: User

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 ');

?>

Related Article

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.