(PHP 4, PHP 5)

die等同于 exit()


语法结构等同于 exit().

add a note add a note

User Contributed Notes 9 notes

Hayley Watson
2 years ago
It is poor design to rely on die() for error handling in a web site because it results in an ugly experience for site users: a broken page and - if they're lucky - an error message that is no help to them at all. As far as they are concerned, when the page breaks, the whole site might as well be broken.

If you ever want the public to use your site, always design it to handle errors in a way that will allow them to continue using it if possible. If it's not possible and the site really is broken, make sure that you find out so that you can fix it. die() by itself won't do either.

If a supermarket freezer breaks down, a customer who wanted to buy a tub of ice cream doesn't expect to be kicked out of the building.
Damien Bezborodov
5 years ago
Beware that when using PHP on the command line, die("Error") simply prints "Error" to STDOUT and terminates the program with a normal exit code of 0.

If you are looking to follow UNIX conventions for CLI programs, you may consider the following:

(STDERR, "An error occurred.\n");
1); // A response code other than 0 is a failure

In this way, when you pipe STDOUT to a file, you may see error messages in the console and BASH scripts can test for a response code of 0 for success:

rc@adl-dev-01:~$ php die.php > test
An error occurred.
rc@adl-dev-01:~$ echo $?

Ideally, PHP would write all Warnings, Fatal Errors, etc on STDERR, but that's another story.
jbailey at raspberryginger dot com
8 years ago
die doesn't prevent destructors from being run, so the script doesn't exit immediately, it still goes through cleanup routines.
2 years ago
Something to keep in mind OWASP states that "Attackers love the extra information error messages provide".  They ask
"Is your error handling set up to prevent stack traces and other overly informative error messages from leaking"
OWASP has guidelines on their site about good error messaging. Something everyone should consider when creating sites.
matthias dot schniedermeyer at telefonica dot de
11 years ago
To get a perl-like die you can/should append this snipped
. " File: " . __FILE__ . " on line: " . __LINE__
die ("Error" . " File: " . __FILE__ . " on line: " . __LINE__);
2 months ago
die is NOT an alias of exit

exit's argument is the script return code

die's argument is an error string that will be printed. die exits with status 0
Thomas Horsten
1 year ago
Because die() still allows destructors to be read, after analyzing a huge dataset (more than 12GB) in RAM, and printing the results, it was taking forever to exit while cleaning up (almost a minute), time that could have been spent running the next dataset. I noticed that killing the process solved this, but I needed to do it automatically from inside the script to do batch jobs. Saved me hours when I found out how. There should be an option to quit without wasting time on destructors. Anyway:

posix_kill(getmypid(), SIGTERM);
10 years ago
Perhaps the Coldfusion query below can be answered as follows:
(From the "User Contributed Notes" for the PHP construct "exit")

nospam at mydomain dot com
27-Sep-2004 10:12
Using return instead of exit is to prefer when you want the script that you have included inside another script to die but continue to execute the main script.
// Radcliff
zero dot affect at hotmail dot com
4 years ago
I was making a few functions and i thought it would be easier to use a single function to close off my code, The following is a example of it working i am sure if you include footer code when exiting code all the time you'll understand the purpose of the following function.

function error_msg($text) {
# add other stuff you may want here
$hello_var = 'hello'; //example of addon to beginning
$goodbye_var = 'goodbye'; //example of addon to end
die($hello_var.'<br />'.$text.'<br />'.$goodbye_var);

error_msg('how are you?');

how are you?
To Top