HOME HTML CSS JAVASCRIPT SQL PHP JQUERY XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

PHP set_error_handler() Function


PHP Error Reference Complete PHP Error Reference

Definition and Usage

The set_error_handler() function sets a user-defined function to handle errors.

This function is used to create your own way of handling errors during runtime.

This function returns the old error handler, or NULL on failure.

Syntax

set_error_handler(error_function,error_types)

Parameter Description
error_function Required. Specifies the function to be run at errors
error_types Optional. Specifies on which errors report levels the user-defined error will be shown. Default is "E_ALL".  See table below for possible error report levels

error_function

Syntax

error_function(error_level,error_message,
error_file,error_line,error_context)

Parameter Description
error_level Required. Specifies the error report level for the user-defined error. Must be a value number. See table below for possible error report levels
error_message Required. Specifies the error message for the user-defined error
error_file Optional. Specifies the filename in which the error occurred
error_line Optional. Specifies the line number in which the error occurred
error_context Optional. Specifies an array that points to the active symbol table at the point the error occurred. In other words, error_context will contain an array of every variable that existed in the scope the error was triggered in

Error Report levels

Value Constant Description
2 E_WARNING Non-fatal run-time errors. Execution of the script is not halted
8 E_NOTICE Run-time notices. The script found something that might be an error, but could also happen when running a script normally
256 E_USER_ERROR Fatal user-generated error. This is like an E_ERROR set by the programmer using the PHP function trigger_error()
512 E_USER_WARNING Non-fatal user-generated warning. This is like an E_WARNING set by the programmer using the PHP function trigger_error()
1024 E_USER_NOTICE User-generated notice. This is like an E_NOTICE set by the programmer using the PHP function trigger_error()
4096 E_RECOVERABLE_ERROR Catchable fatal error. This is like an E_ERROR but can be caught by a user defined handle (see also set_error_handler())
8191 E_ALL All errors and warnings, except level E_STRICT (E_STRICT will be part of E_ALL as of PHP 6.0)


Tips and Notes

Tip: The standard PHP error handler is completely bypassed if this function is used, and the user-defined error handler must terminate the script (die() ) if necessary

Note: If errors occur before the script is executed the custom error handler cannot be used since it is not registered at that time


Example

<?php
//error handler function
function customError($errno, $errstr, $errfile, $errline)
  {
  echo "<b>Custom error:</b> [$errno] $errstr<br />";
  echo " Error on line $errline in $errfile<br />";
  echo "Ending Script";
  die();
  }

//set error handler
set_error_handler("customError");

$test=2;

//trigger error
if ($test>1)
  {
  trigger_error("A custom error has been triggered");
  }
?>

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

Custom error: [1024] A custom error has been triggered
Error on line 19 in C:\webfolder\test.php
Ending Script


PHP Error Reference Complete PHP Error Reference

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates

SHARE THIS PAGE