Modification method: 1. In php.ini, find and modify the value of the "error_reporting" item, and then restart the web server. 2. In the PHP file, use the "error_reporting(level)" statement to modify. The parameter level is used to specify the error reporting level of the current script.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
Error reporting level: Specifies under what circumstances , errors in the script code (errors here are generalized errors, including E_NOTICE, E_WARNING, E_ERROR fatal errors, etc.) will be output in the form of an error report.
How to modify the error level in php
1. Modify the PHP configuration file php.ini
After setting error_reporting in this way, restart the web server and it will take effect permanently.
Taking the xampp integrated software package as an example, open the configuration file php.ini and check the default value of error reporting level error_reporting, as follows:
error_reporting=E_ALL & ~E_DEPRECATED & ~E_STRICT
means reporting all errors except E_DEPRECATED and E_STRICT.
Modify it to:
error_reporting=E_ALL & ~E_NOTICE
It means reporting all errors except E_NOTICE. This is also the most commonly used error reporting level. It will not report errors of the attention class (such as using an undefined variable).
Save it and it will take effect after restarting the web server.
2. Use the error_reporting() function
After setting in this way, it can take effect immediately. But only in the area behind the error_reporting() function call in the current script.
error_reporting(level)
The level parameter specifies the error reporting level of the current script and can be omitted. Both value numbers and constant names are accepted, however, for compatibility with future PHP versions, it is recommended to use constant names.
The level parameter can be an integer or the corresponding constant identifier. It is recommended to use the constant form. The return value is the value (integer value) of the error reporting level in effect at the current location.
The following are some error reporting levels:
Value | Constant | Description |
---|---|---|
1 | E_ERROR | Fatal error at runtime. Unfixable errors. Stop executing the script. |
2 | E_WARNING | Non-fatal runtime error. Script execution is not stopped. |
4 | E_PARSE | Parse error during compilation. Parsing errors should only be generated by the parser. |
8 | E_NOTICE | Runtime notification. Script discovery can be a bug, but can also occur when running a script normally. |
16 | E_CORE_ERROR | Fatal error when starting PHP. This is just like PHP core's E_ERROR. |
32 | E_CORE_WARNING | Non-fatal error when starting PHP. This is just like PHP core's E_WARNING. |
64 | E_COMPILE_ERROR | Fatal error during compilation. This is just like the E_ERROR generated by the Zend scripting engine. |
128 | E_COMPILE_WARNING | Non-fatal error during compilation. This is like an E_WARNING generated by the Zend scripting engine. |
256 | E_USER_ERROR | User-generated fatal error. This is like the E_ERROR generated by the programmer using the PHP function trigger_error(). |
512 | E_USER_WARNING | User-generated non-fatal error. This is like an E_WARNING generated by the programmer using the PHP function trigger_error(). |
1024 | E_USER_NOTICE | User-generated notification. This is like the E_NOTICE generated by the programmer using the PHP function trigger_error(). |
2048 | E_STRICT | Runtime notification. PHP recommends that you make changes to your code to improve code interoperability and compatibility. |
4096 | E_RECOVERABLE_ERROR | Catchable fatal error. This is like an E_ERROR that can be caught by a user-defined handle (see set_error_handler()). |
8191 | E_ALL | All error and warning levels except E_STRICT (E_STRICT will be part of E_ALL as of PHP 6.0). |
示例:
error_reporting(E_ALL ^ E_NOTICE); // 除了E_NOTICE之外,报告所有的错误 error_reporting(E_ERROR); // 只报告致命错误 echo error_reporting(E_ERROR | E_WARNING | E_NOTICE); // 只报告E_ERROR、E_WARNING 和 E_NOTICE三种错误
注意:配置文件php.ini中display_errors
的默认值为On,代表显示错误提示,如果设置为Off,就会关闭所有的错误提示。
使用 error_reporting(0)
或者在函数前面加 @
,可以抑制错误输出,以防止错误消息泄露敏感信息。
推荐学习:《PHP视频教程》
The above is the detailed content of How to modify the error level in php. For more information, please follow other related articles on the PHP Chinese website!