PHP communication, PHP blog, how PHP sets error levels, PHP error information settings, PHP error reports and error information settings, and PHP error report settings
In PHP website development, debugging and solving errors (Bugs) are essential. in the website debugging stage, error messages can be of great help to us. when the website goes online, should we also display the original error information in front of the user? The answer is No. to improve the user experience, we need to set the type and level of the PHP error report and notify the user of the error information in a reasonable way, at the same time, it also avoids providing valuable information to malicious users, such as file paths and database information. The following describes the error reports in PHP and how to set error reports.
Error reports in PHP
You need to understand different types of errors before handling error information. There are two main methods to set the PHP error report, one is through the PHP configuration file PHP. you can use the error_reporting () function to set the error report type to control the error level and display error information.
In PHP. INI, how do I set the error report level?
Open the PHP. ini file and find error_reporting = E_ALL &~ E_NOTICE: this is the default error report setting. the bitwise operation is used to report all the errors except E_NOTICE. if you do not want to display the warning information, you can use the "|" operator: error_reporting = E_ALL &~ (E_NOTICE | E_WARNING ).
ERROR_REPORTING () function setting error report
The error report type set by this function will overwrite the error report settings in the configuration file PHP. ini. This function parameter can be a string or an integer. For example
Error_reporting (3 );
Equivalent
Error_reporting ("E_ERROR | E_WARNING ");
If you do not want to report any errors, you can use error_reporting (0.
Of course, as a PHP developer in PHP website development, you should set error_reporting to E_ALL.
Others
Through the phpinfo function, we can see the display_errors option, which can also set whether to display the error message to the user.
Troubleshooting of error messages
After setting the type and level of the error report, how can I handle the error information to improve the user experience?
In terms of program code, you can use @ to suppress irrelevant errors. you can use custom error handling functions to suppress important error messages, mainly using the set_error_handler function, PHP5 can also handle exceptions.
In front-end display, it is best not to display the original error information to the user, but to notify the user through page navigation and artistic text information.
The above is a basic introduction to error report settings and error message processing in PHP.