PHPMachine Error Handler

Screenshot
Above: Source code display with variable tool tips.


This code snippet can be used to trap errors in your PHP scripts. The main function is GetErrorDescription, it takes five arguments, the error type, the error message, the file the error occurred in, the line number where the error occurred, and an array containing the variables initialized and available at the time of the error. Each of these parameters is provided by PHP whenever an error occurs. The function returns HTML content that can be displayed on the page, saved to a file, e-mailed to the webmaster, or anything else that you can think of!

The output will be split into three sections; an error overview, a section of source code from around the area where the error occurred (you may hover your mouse over a variable to see it's contents) and a collapsible tree view of all initialized variables. Note: the JavaScript used to generate the variable tree is only compatible with Microsoft Internet Explorer.


Screenshot
Above: Collapsible tree
view of all variables.

A very simple example of how to use the code is shown below.

include_once("phpmachine-error-handler.php");
function 
customerror($type, $msg, $file, $line, $context){
   
@ob_end_clean(); // get rid of any half-parsed page
   echo 
GetErrorDescription($type, $msg, $file, $line, $context);
   die();
}
// define custom error handler
set_error_handler
("customerror");


ob_start
();
// ...
// code that may cause an error goes here..
// ...
trigger_error
("This is a test error");
// ...
// more code...
// ...
ob_end_flush
();

This example will display an error report in the browser window whenever any type of error occurs. Obviously this would only be recommended during development - you don't want your users seeing your source code!

Download:

phpmachine-error-handler.php (6KB)

If you have any comments/suggestions please send a mail to info@phpmachine.com. Oh, and while were on the subject of errors... if you need help fixing a broken PHP script or you need a PHP script modified, or even developed
from scratch visit PHPMachine for a free no-obligations quotation. We guarantee that our price will be the cheapest that you find.