PHP: does () die?

Is it bad practice to let die () live in a production environment? It just happened to read this article http://www.phpfreaks.com/blog/or-die-must-die , where the author longs for people who use such a thing in a production environment. Therefore, I should not code this method:

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

How do you code?

+6
php mysql
source share
3 answers

You don't die every time you make a mistake, right? Why do you need your application?

the correct way is to catch errors and process them depending on the context, 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); } } 
+3
source share

die() is a very rude statement ... It’s useful (completely) at the development stage, I found it wrong at the production stage.

You must analyze, track and report fatal errors and display adequate messages such as "Unable to connect to the server, try for several minutes or write to admin@yourhost.com to notify the problem"!

+3
source share

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

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

+1
source share

All Articles