PHP Errors (Cont.)


Creating a Custom Error Handler
A custom error handler creates a special function that can be called when an error occurs in PHP. This example checks whether the given URL starts with the “http://.” It uses the following functions:
 <html><body>
 <?php
   // Error handler function 
   function customError( $errno, $errstr ) { 
     echo "<b>Error:</b> [$errno] $errstr<br />";
     echo "Ending Script";
     die( );
   }
 
   // Set error handler 
   set_error_handler( "customError", E_USER_WARNING );
 
   // Trigger error
   $URL = "";
   if ( strncmp( $URL, "http://", 7 ) == 0 ) 
     header( "Location: $URL" );
   else
     trigger_error( "The URL must start with 'http://'", E_USER_WARNING );
 ?>
 </body></html>
       

These error report levels are the different types of error the user-defined error handler can be used for:

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_NOTIC 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.
8191 E_ALL All errors and warnings, except level E_STRICT.




      I know what men want. Men want to be really,    
      really close to someone who will leave them alone.    
      — Elayne Boosler