PHP set_error_handler() Function

## PHP Basic

PHP HOME PHP Intro PHP Install PHP Syntax PHP Variables PHP Echo / Print PHP Data Types PHP String Functions PHP Constants PHP Operators PHP If...Else...Elseif PHP Switch PHP While Loops PHP For Loops PHP Functions PHP Arrays PHP Sorting Arrays PHP Superglobals

## PHP Forms

PHP Form Handling PHP Form Validation PHP Form Required PHP Form URL/E-mail PHP Form Complete

## PHP Advanced

PHP Arrays Multi PHP Date PHP Include PHP File PHP File Upload PHP Cookies PHP Sessions PHP E-mail PHP Secure E-mail PHP Error PHP Exception PHP Filter

## PHP Database

PHP MySQL Intro PHP MySQL Connect PHP CREATE DB/Table PHP INSERT INTO PHP SELECT PHP WHERE PHP ORDER BY PHP UPDATE PHP DELETE PHP ODBC

## PHP XML

XML Expat Parser XML DOM XML SimpleXML

## PHP and AJAX

AJAX Intro AJAX PHP AJAX Database AJAX XML AJAX Live Search AJAX RSS Reader AJAX Poll

## PHP Reference

PHP Array PHP Calendar PHP Date PHP Directory PHP Error PHP Filesystem PHP Filter PHP FTP PHP HTTP PHP Libxml PHP Mail PHP Math PHP Misc PHP MySQLi PHP SimpleXML PHP String PHP XML PHP Zip PHP Timezones

## PHP Quiz

PHP Quiz PHP Certificate

# PHP set_error_handler() Function

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

Complete PHP Error Reference

## Your suggestion:

Your E-mail (optional):

Page address:

Description:

Close [X]

## Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

## WEB HOSTING

Best Web Hosting UK Reseller Hosting Wow! $5 SSD Hosting

## WEB BUILDING

Download XML Editor
FREE Website BUILDER FREE Website Creator

## WEB RESOURCES

Web Statistics Web Validation

## SHARE THIS PAGE