The page is usually blank when a fatal error occurs in the program. It is not difficult to get the error information! It mainly uses two functions:
Error_get_last () gets the last error message. The structure is as follows:
The code is as follows: |
Copy code |
Array ( [Type] => 8 [Message] => Undefined variable: http://www.111cn.net [File] => C: WWWindex. php [Line] => 2 )
|
Register_shutdown_function () registers a callback function when the script is stopped.
With these two functions, you can monitor fatal errors:
The code is as follows: |
Copy code |
Error_reporting (E_ALL); // E_ALL Function cache_shutdown_error (){ $ _ Error = error_get_last (); If ($ _ error & in_array ($ _ error ['type'], array (1, 4, 16, 64,256,409 6, E_ALL ))){ Echo '<font color = red> your code has an error: </font> </br> '; Echo 'fatal error: '. $ _ error ['message'].' </br> '; Echo 'File: '. $ _ error ['file'].' </br> '; Echo 'in the'. $ _ error ['line']. 'line </br> '; } } Register_shutdown_function ("cache_shutdown_error "); |
Sequence attached local server test method
The following describes three methods for displaying the PHP error message.
I. php. ini configuration
In the php. ini configuration, there are two configuration variables related to this. The two variables and their default values are as follows:
The code is as follows: |
Copy code |
Display_errors = Off Error_reporting = E_ALL &~ E_NOTICE |
The purpose of the display_errors variable is obvious-it tells PHP whether an error is displayed. The default value is Off. The purpose is to display the error prompt:
The code is as follows: |
Copy code |
Display_errors = On |
E_ALL: this setting displays all the information from bad code practices to harmless prompts to errors. E_ALL is a little too detailed for the development process because it displays a prompt even if the variable is not initialized, which is a feature of PHP "advanced. Fortunately, the default value of error_reporting is "E_ALL &~ E_NOTICE ", so that only the error and bad code are displayed, and no negative prompt is displayed for the program.
After modifying php. ini, you need to restart Apache to take effect in apache. Of course, if you test the program only in the command line, you do not need this step.
Configuring the php program
The code is as follows: |
Copy code |
<? Php // Disable error reporting Error_reporting (0 ); // Report running errors Error_reporting (E_ERROR | E_WARNING | E_PARSE ); // Report all errors Error_reporting (E_ALL ); ?> |