Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Handling errors nicely in PHP

Tuesday, August 18th, 2015 by Servage

php-buttonWorking with PHP requires you to consider how to handle errors. The errors can be various kinds and have different consequences for the code execution, and have to be handled accordingly. Errors are generally speaking scenarios where the code encounters a problem because of a given scenario that does not meet the criteria and expectations given by the code. There is no way you can expect to develop a script that never encounters errors, and therefore you should put proper care into your error handling process. If not, then users may experience odd and unexpected behaviour which frustrates and leads to lower satisfaction. More severely it may even lead to broken data and unusable databases.

Input based errors

Some errors are generated by the user entering invalid or unsupported information through inputs. For example a text string in a field that expects a number, or an invalid URL request. These kinds of errors should be caught through proper input validation, so an invalid user based input never escalates to a more severe problem. Just remember to never trust input data. Always validate.

Data type based errors

The data type based errors are usually happening in situations with poor programming, where some data types are not supported – or multiple data types could occur for the same programming variable. PHP is pretty large when it comes to automatically juggling data types, but unfortunately that leads to lazy developers. Therefore be careful about your programming and always consider the proper data type for a given variable. If possible, avoid situations where a variable can be of multiple types.

Service based errors

When using other services (APIs) you must expect that sometimes they do not return the expected data or are simply offline. Therefore consider both availability and data integrity when implementing other services.

Hardware based errors

Errors due to hardware failures are hard to solve with PHP programming. You cannot really do anything if a server fails, and usually your script will not even execute because the server is offline. Therefore these kinds of errors are best handled differently. With your hosting account hardware failures will be taken care of by the server administrators for you.

Handling errors with exceptions

In PHP you can make use of exceptions to throw messages with error messages. This is a good an efficient way to build scripts that can handle errors and degradation gracefully. That means provide decreased functionality or error messages without simply just stopping.

try
{
	// Do your code
	// ...	
	// Throw an exception if you encounter a problem
	throw new Exception('Ooops, something went wrong!');
}
catch (Exception $e)
{
    echo 'Exception message: '.$e->getMessage();
}

The above code shows an example of a try/catch construct in PHP. The code “tries” to do something, but will jump into the “catch” section if an error is thrown. This makes it possible to handle errors by doing something else if an error occurred.

Handling errors nicely in PHP, 5.0 out of 5 based on 5 ratings
Categories: Guides & Tutorials

Keywords:

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.