Specifications
Logical actions might include
• Displaying the error message provided
• Storing information in a log file
• Emailing the error to an address
• Terminating the script with a call to exit
Listing 23.2 contains a script that declares an error handler, sets the error handler using
set_error_handler(), and then generates some errors.
LISTING 23.2 handle.php—This Script Declares a Custom Error Handler and Generates
Different Errors
<?
// The error handler function
function myErrorHandler ($errno, $errstr)
{
echo “<br><table bgcolor = ‘#cccccc’><tr><td>
<P><B>ERROR:</B> $errstr
<P>Please try again, or contact us and tell us that
the error occurred in line “.__LINE__.” of file ‘“.__FILE__.”’”;
if ($errno == E_USER_ERROR||$errno == E_ERROR)
{
echo “<P>This error was fatal, program ending”;
echo “</td></tr></table>”;
//close open resources, include page footer, etc
exit;
}
echo “</td></tr></table>”;
}
// Set the error handler
set_error_handler(“myErrorHandler”);
//trigger different levels of error
trigger_error(“Trigger function called”, E_USER_NOTICE);
fopen(“nofile”, “r”);
trigger_error(“This computer is beige”, E_USER_WARNING);
include (“nofile”);
trigger_error(“This computer will self destruct in 15 seconds”,
E_USER_ERROR);
?>
The output from this script is shown in Figure 23.1.
Debugging
C
HAPTER 23
23
DEBUGGING
493
29 7842 CH23 3/6/01 3:41 PM Page 493










