Writing Solid PHP Code 1 - Error Handling
January 20, 2009
This is the first in a multiple part series on writing solid PHP code. By solid I mean fast, efficient, maintainable and secure code.
This first article deals with error handling. Error handling can stop problems in your application early in their tracks and take appropriate action. Error handling has the added benefit of adding professionalism and also making debugging easier.
Error handling should be applied to any action that has the possibility of failing. Failures in your application are often the result of some unexpected user input, user input should hence always be checked for vailidity.
The most simplistic way of employing error handling is using die(). die() takes a single string argument that it will output and then terminate the script.
Example:
<?php mysql_connect("localhost", "root", "password") or die(mysql_error()); echo "Connected to MySQL the MySQL server! <br \>"; mysql_select_db("dbtable") or die(mysql_error()); echo "Connected to Database"; ?>
The above example tries to connect to a database, and produces the MySQL error if it failed. It then tries to select a table within the database and also produces the MySQL error if it fails. These are both actions that could fail for many reasons: the connection to the the login information could be incorrect, the connection to the database could be lost, or the table might not exist. Even in this short example there are many reasons why it could potentially fail.
Since this example does only two things, it is okay to terminate the script upon failure. If we have a larger script, however, we may want to note the error and continue running. This is where die() falls short, not only can we not continue executing but the message is outputted to the page. If our script produces an error we want to log it for the administrator to view later as well as keep it hidden from our users.
<?php mysql_connect("localhost", "root", "password") or trigger_error(mysql_error(), E_USER_ERROR); echo "Connected to MySQL the MySQL server! <br \>"; mysql_select_db("dbtable") or trigger_error(mysql_error(), E_USER_ERROR); echo "Connected to Database"; ?>
This example uses the trigger_error() function to throw the error. An error will terminate the script while warnings and notices will allow the script to continue running. Below is a list of types of errors you can throw with trigger_error(). Also note an error can have multiple error types.
- E_WARNING
- E_NOTICE
- E_USER_ERROR
- E_USER_WARNING
- E_USER_NOTICE
For a full list, visit the PHP documentation. Note that not all of the error types in the PHP documentation can be used with trigger_error().
For some errors, notices, and warnings will be outputted to the page. This is good for debugging but bad for live sites. To change this open your php.ini file in your php directory and search for the following line
display_errors = OnChange 'On' to 'Off' and save the configuration file and restart your web server. Make sure log_errors is set to 'On' so that all the errors are saved to a log file for you to review later.
Look for more articles on writing solid PHP code.
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.











