PHP error types and handling, PHP error types
1. PHP error level
E_ERROR: a serious error occurs. The script is terminated.
E_WARNING warning, non-serious error, script continues to execute
E_NOTICE prompt, not very important
Code instance
1 echo gettype ($ a); echo "continue execution after E_NOTICE"; // pass an undefined variable, which is a small problem in PHP 2 3 echo gettype (); echo "continue execution after E_WARNING"; // The parameter is not passed to the function normally. This generates a warning 4 5 echo get3 (); echo "E_ERROR and continue execution "; // call a function that does not exist. This is an error. |
Result
We can see that after NOTICE and WARNING, the statement continues to be executed, and the statement after ERROR is not executed. If you change the code of line 5th to line 1st, neither of the following two statements will be executed.
2. Disable the Report of the error message
When these errors occur, the script outputs an error message to the screen, and the execution of the script is terminated due to a serious error.
If you do not want PHP to output error messages to the page, you can set display_errors = on (default) to display_errors = off in the configuration file php. ini.
3. Adjust the error report level
You can modify the error_reporting value in the configuration file php. ini and set PHP to only report errors of a specific level.
For example:
Error_reporting = 0 is equivalent to display_errors = off and no error is reported.
Error_reporting = level constant 1 | level constant 2 | level constant 3. Only errors of these three levels are output.
Error_reporting = E_ALL &~ (Level constant 1 | level constant 2 | level constant 3), indicating that only errors of these three levels are not reported.
If you do not want to modify the configuration file, you can directly modify it using the error_reporting () function in the script.
4. custom error handling
When an error occurs, PHP reports an error directly in the wrong place. This may affect the page layout, and exposing the error information directly may cause risks, therefore, try to use a function to handle errors.
The set_error_handler (funcname) function is used to register an error handler. The funcname () function must have four parameters, indicating the error level, error information, error file, and error line.
The following is an instance. Set a function error_hand () to save all the error messages until the end of the page is output.
1 <? Php 2 $ error_message = ''; 3 set_error_handler ('error _ hand'); 4 5 function error_hand ($ level, $ message, $ file, $ line) 6 {7 global $ error_message; 8 9 $ error_message. = "error level :". $ level. "error message :". $ message. "error file :". $ file. "Error row :". $ line. "<br>"; 10 11} 12 13 echo gettype ($ a); echo "E_NOTICE and continue execution <br>"; // pass an undefined variable, this is a small problem in PHP 14 15 echo gettype (); echo "E_WARNING and then continue to execute <br>"; // The parameter is not passed to the function normally, this generates a warning 16 17 18 echo "-- ---------------------------------------------------------------------- <Br> "; 19 echo $ error_message; 20?> |
Result
Note:
Code China deletes the line that would have generated an ERROR. If this line is not deleted, the Code cannot be executed normally, this is because the High-level errors such as E_ERROR will not be processed by this handle.
After set_error_hand is used, error_reporting will become invalid, that is, all error messages will be handed over to the custom function for processing.