The difference between die and exit in PHP

Source: Internet
Author: User
Tags exit in
Think of a problem first:

What does the following code show to the page?

1

2 die(123);

3 ?>

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:

1

2 echo'123';

3 die();

4 ?>

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

1 echo"1111";

2 exit(0);

3 echo"2222"; // 22222不会被输出,因为程序运行到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 ().

1 $fp=fopen("./readme.txt","r")ordie("不能打开该文件");

2 // 这种情况下,如果fopen函数被调用返回布尔值false时,die()将立即终止脚本,并马上打印

3 // 传递给它的字符串,“死前还能说一两句话”。

Back to the previous topic, why does the following code not output 123 to the page?

1

2 die(123);

3 // 或 exit(123);

4 ?>

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

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:

1

2 exit('123');

3 ?>

    • 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

1

2 die('123');

3 ?>

The above describes the difference between die and exit in PHP, including the contents of the content, I hope that the PHP tutorial interested in a friend to help.

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