简体   繁体   中英

PHP: Does die() must die?

Is it considered bad practice to let die() live in a production enviroment? Just happened to read this article http://www.phpfreaks.com/blog/or-die-must-die where the author fies upon people who use such a thing in a production enviroment. So shouldn't I code this way:

$connection = mysql_connect($db_host, $db_username, $db_password);
if (!$connection){
    die ("Could not connect to the database.");
}

How do you code?

die() is a very rough statement... Useful (quite) in developer stage, i found it wrong in production stage.

You should analyze, monitor and log fatal errors, and displaying adequate messages like "It's impossible to connect to the server, please try in few minutes or write to admin@yourhost.com to notify the problem"!

You don't die everytime you make a mistake, do you. Why should your application do?

the correct way is to intercept errors and process them in a context-dependent fashion, for example

try {
    application goes here

    $conn = mysql_connect(...)
    if(!$conn)
        throw ....
    ....
} catch(Exception $err) {
     if(PRODUCTION) {
        log error
        say something nice
     }
     if(DEBUG) {
         var_dump($err);
     }
}

Well in a productive enivornment, you should never expose any errors/information about the system to the outside world.

Important is, to log all errors. If you are talking about a website, I would send an HTTP status 500 as response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM