Computer >> Computer tutorials >  >> Programming >> PHP

set_error_handler() function in PHP


The set_error_handler() function sets a user-defined function to handle errors. It returns a string containing the previously defined error handler (if any), or NULL on error.

Syntax

set_error_handler ( error_handler, error_types );

Parameters

  • error_handler − Specifies the function to be run at errors. Syntax of error_handler is given below.

  • error_types − Specifies on which errors report levels the user-defined error will be shown. Default is "E_ALL".

Return

The set_error_handling() function returns a string containing the previously defined error handler (if any), or NULL on error.

Example

The following is an example −

<?php
function customError($errno, $errstr, $errfile, $errline) {
   echo "Custom error: [$errno] $errstr\n";
   echo "Error on line $errline in $errfile\n";
   echo "Ending Script"; die();
}
//set error handler set_error_handler("customError");
$test = 0; //trigger error if ($test > -1) {
   trigger_error("A custom error has been triggered");
}
?>

Output

Custom error: [1024] A custom error has been triggered
Error on line 16 in /home/cg/root/1531703/main.php Ending Script