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?

Comments

It is bad practice to show end users your database errors - those should be logged some how, and a generic error shown to you users.

Written by Matt Ellen

By the way, the best topic-title ever!

Written by Enrico Carlesso

Please see stackoverflow.com/questions/2130105/…

Written by LiraNuna

Accepted Answer

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);
     }
}
Written by stereofrog
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki