First, error handling in PHP
1. Error level in PHP
2. To adjust the PHP error reporting level >>>php, there are two ways to adjust the error reporting level: ① Modify the configuration entry for the php.ini file. A. Causes all PHP files under the current server environment to be affected B. If the code is to replace the server, it will cause the configuration file to be completely invalid, need reconfiguration Therefore, in the actual development process, it is not recommended this way of modification ② in the code, using the Ini_set () function, dynamic modification php.ini Configuration items for files >>> Two important configuration items in php.ini ①display_errors: Turn on or off all error level optional values: 1/on to turn on error reporting 0/off to turn off all reports is on by default and strong Recommended to stay open
Ini_set ("Display_errors", "0"); Turn off all Error reporting features
②error_reporting: Set those errors, can be reported, there are two functions can be set ini_set (); Error_reporting ();
Ini_set ("Error_reporting", "E_all"); // Report All Errors error_reporting // do not report attention to notice level, other error normal report
3. Use the error log to log errors ① use log file records: Use the Error_log () function to output error information to the log file, the default log file in Wamp, under the log folder, Php_error.log file >>> Custom log file Printing:
Ini_set // set the log file address Error_log // Print an error message Error_log // Print errors directly to the specified file
② write into Windows system log
Ini_set // set up logging to the system log Error_log // when using the log error, the system log will be automatically entered
4, Trigger_error (): Custom an Error! will be the same as the error of the system, resulting in errors. Parameter A error level of the information content parameter B error. E_user_warning E_user_error5, custom error handling function: ① declares an error handler function func () ② uses Set_error_handler ("Func"); Set when the file error, no longer use the system's own error prompt function, but call our custom function >>> Set_error_handler when the function is triggered, the default is to send four parameters to the function, namely: the wrong level, the wrong information, The file where the error occurred, the line number of the error so you can receive the error message when declaring the function, specifically processing.
$errMessage= ""; Ini_set("Log_errors", "on"); functionErrorHandler ($type,$message,$file,$line){ Global $errMessage; Switch($type) { Case E_notice:$errMessage. = "There is no small bug in the egg, do not have to deal with the <br><br>"; Break; Case e_warning:$errMessage. = "Error!!! "; $errMessageThe. = "Error type is: {$type}"; $errMessageThe. = "error message is: {$message}"; $errMessage. = "The file where the error occurred is: {$file}"; $errMessage. = "The line number where the error occurred is: {$line}"; $errMessage. = "<br><br>"; Break; Case E_error:Echo"<script>"; Echo"Location.href=". /test.php ' "; Echo"</script>"; Break; default:Echo"<script>"; Echo"Location.href=". /test.php ' "; Echo"</script>"; Break; } }Set_error_handler("ErrorHandler");
Error Handling in PHP