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

Source: Internet
Author: User
Think of a problem first:

What does the following code show to the page?

The code is as follows

Die (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:

The code is as follows

echo ' 123 ';

Die ();

?>

A piece of information on the Web:

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 passed to the exit and Die function, the value of 0 o'clock means that the execution of the script is terminated prematurely, usually with the name exit ().

code as follows

echo "1111";

Exit (0);

echo "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 ().

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 called 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

Die (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 is going to stop running, terminates the script with the exit () function and allows the output point content to run to the environment (typically stored in a global variable), but the output can only be a number, indicating "the end state of the command."

That is, 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 is changed to the following form:

The code is as follows


Exit (' 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

The code is as follows

Die (' 123 ');

?>

http://www.bkjia.com/PHPjc/743706.html www.bkjia.com true http://www.bkjia.com/PHPjc/743706.html techarticle think of a question: what does the following code show to the page? PHP die (123); There was a time when I thought the page would show 123, but the results told me that the answer ...

  • 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.