©
本文档使用 PHP中文网手册 发布
(PHP 4, PHP 5)
die — 等同于 exit()
语法结构等同于 exit() .
[#1] Hayley Watson [2012-08-25 16:29:30]
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.
[#2] Damien Bezborodov [2009-08-20 22:45:09]
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:
<?php
fwrite(STDERR, "An error occurred.\n");
exit(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 $?
1
Ideally, PHP would write all Warnings, Fatal Errors, etc on STDERR, but that's another story.
[#3] jbailey at raspberryginger dot com [2007-01-20 09:33:29]
die doesn't prevent destructors from being run, so the script doesn't exit immediately, it still goes through cleanup routines.