"Organize and share 1" The difference between exit () and Die () in PHP

Source: Internet
Author: User

This article briefly analyzes the differences between the exit and die that are often used in PHP, and a friend who needs to see it.

First, consider a question:

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:

<?phpecho ' 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 ().

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

$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 immediately print//pass the string to it, " I can say one or two words before I die. "

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 be stored in the runtime Environment (typically in a global variable), but the output can only be a number , which means " The end state of the command .

Related Reference Links: http://blog.snsgou.com/post-711.html

That is, exit (123) just outputs a running state 123, Beijing Night Job recruitment [http://www.bblktv.com/] instead of really going to the console to output 123. 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. Beijing Night-Skiing recruitment

<?phpdie (' 123 ');? >

"Organize and share 1" The difference between exit () and Die () in PHP

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.