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

Source: Internet
Author: User
Tags exit execution functions php session reference return string variable

This article briefly analyzes the use of PHP in the exit and die differences, there is a need for friends to see.

First, consider 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 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:

<?php
echo ' 123 ';
Die ();
? >

A piece of information on the Internet:

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

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

$FP =fopen ("./readme.txt", "R") or Die ("Cannot open the file");
In this case, if the fopen function is invoked to return the Boolean value false, Die () will immediately terminate the script and print
//Pass it to the string immediately, "one or two words before death."

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

<?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 operating 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

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:

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

<?php
die (' 123 ');
? >

Articles that you may be interested in

    • The difference and usage of return and exit, break and Contiue in PHP
    • Usage and difference of echo,print,print_r,var_export,var_dump in PHP
    • A detailed explanation of Empty,is_null,isset in PHP
    • Use and difference in PHP to jump out of multiple loops using Break,continue,goto,return,exit
    • The difference between the Execute and query methods in thinkphp
    • JavaScript in Scrollheight,scrollwidth,scrollleft,scrolltop and other differences pee
    • The difference between a variable and a function in PHP after adding the static keyword
    • The difference between a PHP session and a cookie


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.