Home > Backend Development > PHP Tutorial > Detailed explanation of error_reporting() usage in PHP_php skills

Detailed explanation of error_reporting() usage in PHP_php skills

WBOY
Release: 2016-05-16 20:08:13
Original
934 people have browsed it

The error_reporting() function specifies which error to report. This function sets the error reporting level for the current script. This function returns the old error reporting level.

First of all, you must know that the error_reporting() function is used to set the error level and return the current level. It has 14 error levels as follows:

1    E_ERROR     致命的运行时错误。 错误无法恢复过来。脚本的执行被暂停
2    E_WARNING    非致命的运行时错误。 脚本的执行不会停止
4    E_PARSE     编译时解析错误。解析错误应该只由分析器生成
8    E_NOTICE     运行时间的通知。
16    E_CORE_ERROR   在PHP启动时的致命错误。这就好比一个在PHP核心的E_ERROR
32    E_CORE_WARNING  在PHP启动时的非致命的错误。这就好比一个在PHP核心E_WARNING警告
64    E_COMPILE_ERROR 致命的编译时错误。 这就像由Zend脚本引擎生成了一个E_ERROR
128   E_COMPILE_WARNING 非致命的编译时错误,由Zend脚本引擎生成了一个E_WARNING警告
256   E_USER_ERROR   致命的用户生成的错误。
512   E_USER_WARNING  非致命的用户生成的警告。 
1024   E_USER_NOTICE  用户生成的通知。
2048   E_STRICT     运行时间的通知。

4096   E_RECOVERABLE_ERROR 捕捉致命的错误。

8191   E_ALL来     所有的错误和警告。

Copy after login

It seems that php is not enabled by default, so you need to configure the php.ini file:

Change display_errors = Off to display_errors = On

Also configure the error level: change

error_reporting = E_ALL Change to:

error_reporting = E_ALL & ~E_NOTICE

It should be because PHP displays all errors by default, but we don’t need to display some harmless prompts, so the settings are as above!

can also be used in php code as follows:

<&#63;php
//禁用错误报告,也就是不显示错误
error_reporting(0);
//报告运行时错误
error_reporting(E_ERROR | E_WARNING | E_PARSE);
//报告所有错误
error_reporting(E_ALL);
&#63;>

Copy after login

Usage example:

I encountered a problem while learning the CI framework today:

A PHP Error was encountered 
Severity: Notice 

Message: Undefined variable: user 

Copy after login

Generally, no error will be reported when outputting an undefined declared variable in the default ordinary PHP file, but an error will be reported under the codeigniter framework. This is for "lazy people" who want to integrate adding and modifying pages. It’s very inconvenient. Since I’m a beginner, I still want to block this error message in the code. I even used @, but many people said that @ will greatly reduce the performance...

Finally, I suddenly thought, did codeigniter intentionally prompt this error message? How can we block this type of error? I accidentally searched for "How to prevent codeigniter from displaying Notice information?", and it suddenly dawned on me. It turned out to be this type of error. The error_reporting(E_ALL); in the entry index.php is causing trouble. Just change it to
​error_reporting(E_ALL ^ ​​E_NOTICE);
This error can be blocked without affecting other errors.

We may often see such a function in the program

function setErrorReporting()
{
  //从配置文件读取当前是否为开发环境
  if (DEV_ENV == true) {
    ini_set("error_reprorting", "E_ALL & ~E_NOTICE");
    ini_set("display_errors", "on");
  } else {
    error_reporting(E_ALL);
    ini_set('display_errors', 'Off');
    ini_set("log_errors" , "On");
    ini_set('error_log', '/var/log/phperror.log');
  }
}
Copy after login

Example:
In the Windows environment: The program that originally ran normally in php4.3.0, why are there many errors reported in 4.3.1? The general prompt is: Notice: Undefined varialbe: variable name.

For example, the following code:
The code is as follows Copy the code
if (!$tmp_i) {
$tmp_i=10;
}
It runs normally in 4.3.0, but it prompts Notice: Undefined varialbe:tmp_i
when running in 4.3.1. The questions are as follows: 1. Where is the problem?
2. How should this code be modified?
3. Without changing the code, how to modify the settings in php.ini so that the original program in 4.3.0 can run normally in the 4.3.1 environment without this error message.
Solution:

Add a sentence at the beginning of the program:
The code is as follows Copy the code
error_reporting(E_ALL & ~E_NOTICE); or error_reporting(E_ALL ^ ​​E_NOTICE);
Or modify php.ini:
The code is as follows Copy the code
error_reporting = E_ALL & ~E_NOTICE
About the error_reporting() function: error_reporting() sets the error level of PHP and returns the current level.
; Error reporting is bitwise. Or add up the numbers to get the desired error reporting level.
; E_ALL - all errors and warnings
; E_ERROR - fatal runtime error
; E_WARNING - runtime warning (non-fatal error)
; E_PARSE - compile-time parsing error
; E_NOTICE - runtime alerts (These are often caused by bugs in your code, or may be caused by intentional behavior. (eg: using an uninitialized variable based on the fact that it is automatically initialized to an empty string. variable)
; E_CORE_ERROR - Fatal error that occurred during the initialization process when PHP started
; E_CORE_WARNING - Warning (non-fatal error) that occurs during the initialization process of PHP startup
; E_COMPILE_ERROR - Fatal compile-time error
; E_COMPILE_WARNING - compile-time warning (non-fatal error)
; E_USER_ERROR - user generated error message
; E_USER_WARNING - user generated warning message
; E_USER_NOTICE - user generated reminder message
E_NOTICE means that the normal situation is not recorded, and is only used when the program has an error, such as trying to access a non-existent variable, or calling the stat() function to view a non-existent file.
E_WARNING is usually displayed but does not interrupt program execution. This is useful for debugging. For example: calling ereg() with the problematic regular notation.
E_ERROR is usually displayed and will interrupt program execution. This means that memory configuration or other errors cannot be traced using this mask.
E_PARSE Parses errors from the syntax.
E_CORE_ERROR Like E_ERROR, but excludes errors caused by the PHP core.
E_CORE_WARNING Like E_WARNING, but does not include PHP core error warnings

How to use:
error_reporting(0);//Disable error reporting
error_reporting(E_ALL ^ ​​E_NOTICE);//Display all error messages except E_NOTICE
error_reporting(E_ALL^E_WARNING^E_NOTICE);//Display all error messages except E_WARNING E_NOTICE
error_reporting(E_ERROR | E_WARNING | E_PARSE);//Display runtime errors, which has the same effect as error_reporting(E_ALL ^ ​​E_NOTICE);. error_reporting(E_ALL);//Show all errors
error_reporting(0)
error_reporting(255);
Yes list all tips
error_reporting(0);
Do not show all prompts
It is recommended to use
error_reporting(7);
Show only critical errors
1 E_ERROR Fatal runtime error
2 E_WARNING runtime warning (non-fatal error)
4 E_PARSE compile-time parsing error
8 E_NOTICE runtime reminder (often a bug, maybe intentional)
16 E_CORE_ERROR Fatal error during initialization during PHP startup
32 E_CORE_WARNING Warning during initialization process when PHP starts (non-fatal error)
64 E_COMPILE_ERROR Fatal error during compilation
128 E_COMPILE_WARNING compile-time warning (non-fatal error)
256 E_USER_ERROR User-defined fatal error
512 E_USER_WARNING User-defined warning (non-fatal error)
1024 E_USER_NOTICE User-defined reminder (often a bug, maybe intentional)
2048 E_STRICT encoding standardization warning (recommended how to modify for forward compatibility)
4096 E_RECOVERABLE_ERROR A near-fatal runtime error. If not caught, it will be treated as E_ERROR
6143 E_ALL All errors except E_STRICT (8191 in PHP6, including all)

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