note 95988 added to function.die
| From: | (php-net@starurl.com) | Date: | Mon, 01 Feb 2010 21:49:23 +0000 |
| Subject: | note 95988 added to function.die | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-165575@lists.php.net to get a copy of this message | ||
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! :)
----
Server IP: 69.147.83.197
Probable Submitter: 85.210.30.198
----
Manual Page -- http://www.php.net/manual/en/function.die.php
Edit -- https://master.php.net/note/edit/95988
Del: integrated -- https://master.php.net/note/delete/95988/integrated
Del: useless -- https://master.php.net/note/delete/95988/useless
Del: bad code -- https://master.php.net/note/delete/95988/bad+code
Del: spam -- https://master.php.net/note/delete/95988/spam
Del: non-english -- https://master.php.net/note/delete/95988/non-english
Del: in docs -- https://master.php.net/note/delete/95988/in+docs
Del: other reasons-- https://master.php.net/note/delete/95988
Reject -- https://master.php.net/note/reject/95988
Search -- https://master.php.net/manage/user-notes.php