How to use set error handler in PHP

This article describe about set_error_handler() function in PHP.
  • 1716

set_error_handler() function

This function set the user defined function to the handle error.

During runtime this function generate its own way to handle the error.

Old error handler return by this function.

Syntax

set_error_handler (error_function, error_type)

 

Parameter Description
error_function Required. Define the function to run at error.
error_type Optional. Define in which error report level user define error shown.

error_function

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

 

Error Report level

 

Value Constant Description
2 E_WARNING Non fatal runtime error.
8 E_NOTICE Runtime notice.
256 E_USER_ERROR Fatal user generate error.
512 E_USER_WARNING Non fatal user generate error.
1024 E_USER_NOTICE User generated notice.
2048 E_STRICT Runtime notice.
4096 E_RECOVERABLE_ERROR Catchable fatal error.
8191 E_ALL All error and warning. except level E_STRICT

Example

<html>
<body>
<?php
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("customError");

$test=2;
if ($test>1)
{
trigger_error("A custom error has been triggered");
}
?>
</body>
</html>

Output

pic21.jpg

You may also want to read these related articles Click here

Ask Your Question

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.