Home > Backend Development > PHP Problem > How to modify the error level in php

How to modify the error level in php

青灯夜游
Release: 2023-03-09 22:04:01
Original
2872 people have browsed it

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.

How to modify the error level in php

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
Copy after login

means reporting all errors except E_DEPRECATED and E_STRICT.

Modify it to:

error_reporting=E_ALL &  ~E_NOTICE
Copy after login

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)
Copy after login

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:

ValueConstantDescription
1E_ERROR Fatal error at runtime. Unfixable errors. Stop executing the script.
2E_WARNINGNon-fatal runtime error. Script execution is not stopped.
4E_PARSEParse error during compilation. Parsing errors should only be generated by the parser.
8E_NOTICERuntime notification. Script discovery can be a bug, but can also occur when running a script normally.
16E_CORE_ERRORFatal error when starting PHP. This is just like PHP core's E_ERROR.
32E_CORE_WARNINGNon-fatal error when starting PHP. This is just like PHP core's E_WARNING.
64E_COMPILE_ERROR Fatal error during compilation. This is just like the E_ERROR generated by the Zend scripting engine.
128E_COMPILE_WARNINGNon-fatal error during compilation. This is like an E_WARNING generated by the Zend scripting engine.
256E_USER_ERRORUser-generated fatal error. This is like the E_ERROR generated by the programmer using the PHP function trigger_error().
512E_USER_WARNING User-generated non-fatal error. This is like an E_WARNING generated by the programmer using the PHP function trigger_error().
1024E_USER_NOTICE User-generated notification. This is like the E_NOTICE generated by the programmer using the PHP function trigger_error().
2048E_STRICTRuntime notification. PHP recommends that you make changes to your code to improve code interoperability and compatibility.
4096E_RECOVERABLE_ERRORCatchable fatal error. This is like an E_ERROR that can be caught by a user-defined handle (see set_error_handler()).
8191E_ALLAll 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三种错误
Copy after login

注意:配置文件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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template