PHP: Don’t use the die statement.

Please don’t use PHP’s die / exit statement unless it is absolutely necessary. Especially if you are designing libraries or functions that could be used by others.

“or die(mysql_error())”

There are a sizable number of developers who will lazily use the die statement for failed MySQL queries. While working on projects in the past, I have often come across examples such as this:

OK, I get it. The SQL query in question could be extremely important to the logic of your application. So important in fact that you do not want the rest of the script to execute if it fails.

That is completely understandable.

However, the problem with the above piece of code is that it fails silently. If this SQL statement starts to fail, you will not know about it until an end user witnesses the error and points it out to you. If the query fails during a cron job or an Ajax request, it might take quite some time before you realize it isn’t working.

This is because both the exit and die statements terminate the script. They do not log any errors.

A much better approach in the example above would be to combine the mysql_functions with exceptions. That way, you can kill the script, show the user an error page and log the error in question. You can even setup a PHP exception handler and display a custom error page.

All in all, exceptions are a much better solution for error handling.

Let’s face it, die is just ugly.

Pages that spit out errors using the die statement are ugly. When the die statement is used for error handling, the end user is often left with a blank default white page and some text.

This does not make for a good user experience.

What is the user supposed to do? The navigation has disappeared and now they have to hit the back button or leave your website entirely.

Instead, a user should be shown an error page that matches the design of the site that they are using. This can be accomplished by setting up a custom exception handler.

Scripts failing silently.

A while back, one of our PHP cron jobs started to fail. However, it took over a week before we realized that it was happening.

Upon closer inspection, I discovered that the script was being terminated prematurely.

This particular piece of code was processing items in a queue.

When a particular item caused an error, an exception was thrown and caught so that other items in the queue remained unaffected.

However, in this case, one item at the start of the loop / queue was failing and it was preventing other items from being processed.

This created a backlog.

In the end, we discovered that a PDF conversion library that we had downloaded from Github was calling the die statement whenever it encountered an error.

To fix this issue, we had to amend the library to throw an exception instead of calling the die statement.

This is just one example of how the die statement can come back to bite you on the ass. It is also a good example of why you should not be using it in libraries that are going to be used in other projects.

Facebook Comments