News:

Choose a design and let our professionals help you build a successful website   - ITAcumens

Main Menu

The default Error handler for PHP

Started by sajiv, Aug 23, 2008, 04:47 PM

Previous topic - Next topic

sajiv


Set Error Handler
The default error handler for PHP is the built in error handler. We are going to make the function above the default error handler for the duration of the script.

It is possible to change the error handler to apply for only some errors, that way the script can handle different errors in different ways. However, in this example we are going to use our custom error handler for all errors:

Code:
set_error_handler("customError");
Since we want our custom function to handle all errors, the set_error_handler() only needed one parameter, a second parameter could be added to specify an error level.

Example

Testing the error handler by trying to output variable that does not exist:

Code:
<?php
//error handler function
function customError($errno, $errstr)
{
echo "Error: [$errno] $errstr";
}//set error handler
set_error_handler("customError");//trigger error
echo($test);
?>


The output of the code above should be something like this:

Code:
Custom error: [8] Undefined variable: test

:acumen