Home  >  Article  >  Backend Development  >  The difference between die and exit in php

The difference between die and exit in php

WBOY
WBOYOriginal
2016-07-30 13:30:37930browse

Think about a question first:

What will the following code display to the page?

1

2 die(123);

3?>

There was a time when I always thought that the page would display 123, but the practical results told me that the answer was wrong and the page was blank!

I still don’t know why, but 123 is not output. In order to let the page output 123, I modified it to the following code:

1

2echo'123';

3 die();

PHP Manual: die()Equivalent to exit().
4?>

Explanation

: die() and exit() are both functions to terminate script execution; in fact, the two names exit and die point to the same function, and die() is an alias of the exit() function. This function only accepts one parameter, which can be a value returned by a program or a string, or no parameters can be entered, and the result is no return value.


Reference: Although the two are the same, there are subtle selectivity in their usual use. For example:

When the value passed to the exit and die functions is 0, it means to terminate the execution of the script early, usually using the name exit().

1

echo

"1111"

;

(0);
2exit

"2222"
3echo
;

// 22222 will not be output, because when the program runs to exit(0), the script has been terminated early and "will die immediately".

1
When the program errors, you can pass it a string, which will be output as it is on the system terminal, usually using the name die().

$fp

=

fopen

(

"./readme.txt"
,"r ") ordie("Cannot open the file");2// If the fopen function is called When returning the Boolean value false, die() will immediately terminate the script and immediately print the string passed to it, "You can say something before you die" Two sentences".

1
Back to the previous topic, why doesn’t the following code output 123 to the page?

2

die

(123);

// or exit(123);
3

4?>

My own summary:2. PHP has multiple running methods, either in website form or script form (no web server is required). For example, in the Bash Shell scripting language, when it wants to stop running, it will use the exit() function to terminate the script and allow the output content to be run. environment (usually stored in a global variable), but the output content can only be Number, indicating "the end status of the
1. Functionally, die() is equivalent to exit(); When PHP is running as a script, it is recommended to use exit():
command".

Related reference link: http://blog.snsgou.com/post-711.htmlIn other words, exit(123) only outputs a running status 123, rather than actually outputting a string to the console 123. If you want to output 123 to the console, the code must be changed to the following form: 1

2

exit

(

'123'

);

3

?>

  • When PHP is running as a website, it is recommended to use die():

But at this time die (number) is meaningless because it will not output to the page Numeric string, that is to say, if you want the page to terminate and output numbers, you have to change it to the following form

1

2die('123');

3?>

The above introduces the difference between die and exit in PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:clone object in phpNext article:clone object in php