note 95988 deleted from function.die by danbrown
| From: | danbrown@php.net | Date: | Mon, 01 Feb 2010 21:58:30 +0000 |
| Subject: | note 95988 deleted from function.die by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-165576@lists.php.net to get a copy of this message | ||
Note Submitter: BJ (php-net at starurl dot com)
----
Don't use the infamous "or die" for error handling! This causes the script to exit()
with no error code (HTTP status "200 OK") and can't be handled by an error handling
routine. Just as easy but much better is to use "or trigger_error". i.e., change:
@statement or die("my error message");
to:
@statement or trigger_error("my error message");
this has exactly the same effect / usage pattern, but can be caught by custom error routines
(set_error_handler) and returns a non-zero error code to the calling process so it knows an error
occurred (and can log it as such, etc).
AIUI, from PHP v5.2.4, generating a proper error in this way (along with other errors from built-in
routines) will also cause a proper "500 Internal Server Error" HTTP status code, unlike
die/exit(), which return "200 OK".
NB: stupidly, prior to v5.2.4, even normal PHP errors returned HTTP "200 OK"! I'd
love to know how that design decision lasted so long! :)