How to modify the error level in php

青灯夜游
Release: 2023-03-10 09:58:02
Original
2424 people have browsed it

In PHP, you can use the error_reporting() function to modify the error level. This function can specify which error to report. The syntax format is "error_reporting(level)"; the parameter level specifies the error reporting level of the current script, and the value is a number. and constant names are acceptable.

How to modify the error level in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

You can set the server error in php.ini alarm level. By default, PHP will report all errors except notifications.

The error reporting level is set through some predefined constants,

Syntax

int error_reporting ( [int level] )
Copy after login

If the parameter level is not specified, the current error reporting level will be returned. The following items are possible values ​​of level:

As shown in the following table:

1        E_ERROR        报告运行时的致命错误
2        E_WARNING        报告运行的非致命错误

4        E_PARSE        报告解析错误
8        E_NOTICE        报告通告,注意,表示所做的事情可能是错误的.
16           E_CORE_ERROR        报告PHP引擎启动失败
32           E_CORE_WARNING    报告PHP引擎启动时非致命错误

64           E_COMPILE_ERROR    报告编译错误
128         E_COMPILE_WARNING    报告编译时出现的非致命错误

256         E_USER_ERROR        报告用户触发的错误
512         E_USER_WARNING    报告用户触发的警告

1024          E_USER_NOTICE        报告用户触发的通告
2047       E_ALL            报告所有的错误和警告
2048       E_STRICT        报告不赞成的用法和不推荐的行为
Copy after login

In the php.ini file, the default report is all errors except notifications, as set by the following statement :

error_reporting = E_ALL & ~ (E_NOTICE)
Copy after login

In the above expression, the symbol "&" indicates the juxtaposition of several conditions, and "~" indicates negation, which means NOT.

If If you want to ignore the warning message, you can set the following statement:

   error_reporting = E_ALL & ~(E_NOTICE) & ~(E_WARNING)
Copy after login

In the php.ini file, the error-related settings are as follows:

(1)error_reporting, set Set the error level

(2)display_errors, whether to display error reports, set to ON to open, set to OFF to close all error prompts

(3)log_errors, the default setting is OFF, whether Record error logs;

(4)track_errors, the default setting is OFF, this option can help solve errors in the code, rather than letting PHP provide its default functions.

Collected from the Internet:

E_NOTICE 表示一般情形不记录,只有程式有错误情形时才用到,例如企图存取一个不存在的变数,或是呼叫 stat() 函式检视不存在的档案。

E_WARNING 通常都会显示出来,但不会中断程式的执行。这对除错很有效。例如:用有问题的常规表示法呼叫 ereg()。
E_ERROR 通常会显示出来,亦会中断程式执行。意即用这个遮罩无法追查到记忆体配置或其它的错误。
E_PARSE 从语法中剖析错误。
E_CORE_ERROR 类似 E_ERROR,但不包括 PHP 核心造成的错误。
E_CORE_WARNING 类似 E_WARNING,但不包括 PHP 核心错误警告。
Copy after login

PHP error report

There are many configuration settings in the php.ini file. You should have already set up your php.ini file and placed it in the appropriate directory, just like installing PHP on Linux as shown in the Apache 2 documentation. There are two configuration variables that you should be aware of when debugging PHP applications. Here are the two variables and their default values:

display_errors = Off
error_reporting = E_ALL
Copy after login

The current default values ​​of these two variables can be found by searching for them in the php.ini file. The purpose of the display_errors variable is obvious - it tells PHP whether to display errors. The default value is Off. However, to make the development process easier, set this value to On:

display_errors = On
Copy after login

error_reporting The default value of the error_reporting variable is E_ALL. This setting will show everything from bad coding practices to harmless tips to errors. E_ALL It's a bit too granular for development, as it also displays prompts on the screen for small things (such as variables not being initialized), which messes up the browser's output. I just want to see errors and bad coding practices, but not Harmless hint. So, please replace the default value of error_reporting with the following value:

error_reporting = E_ALL & ~E_NOTICE
Copy after login

Restart Apache and you are all set. Next, you'll learn how to do the same thing on Apache.

Error reporting on the server

Depending on what Apache is doing, turning on error reporting in PHP may not work because there may be multiple PHP version. Sometimes it's hard to tell which PHP version

Apache is using because Apache can only look at one php.ini file. Not sure which one Apache is using The php.ini file configuration itself is a security issue. However, there is a way to configure PHP variables in Apache so that the correct error level is set.

Also, it's good to know how to set these configuration variables on the server side to override or preempt the php.ini file, thus providing a higher level of security.

When configuring Apache, you should have already been exposed to the basic configuration in the http.conf file in /conf/httpd.conf.

To do what you have already done in the php.ini file, add the following lines to httpd.conf, overwriting any php.ini files:

php_flag   display_errors         on
php_value error_reporting       2039
Copy after login

This will override any php.ini files. ini file that has been set for display_errors, and the value of error_reporting. Value 2039 Represents E_ALL & ~E_NOTICE. If you prefer to use E_ALL, set the value to 2047. Again, you still need to restart Apache.

Next, you need to test the error reporting on the server.

Regarding the error_reporting() function, it can shield some error messages, but errors caused by the PHP core cannot be shielded, because errors caused by the PHP core will directly cause the PHP file compilation to fail, because writing Errors caused by the format not being written in accordance with PHP encoding rules cannot be blocked

Common ones are as follows:

 // Turn off all error reporting;关闭所有的错误
error_reporting(0);
// Report simple running errors;报告一个简单的运行错误
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings …);包括报告一些未初始化的变量或捕捉变量名的拼写错误
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
// Report all errors except E_NOTICE
// This is the default value set in php.ini;报告所有的错误但不包括E_NOTICE
error_reporting(E_ALL ^ E_NOTICE);
// Report all PHP errors (bitwise 63 may be used in PHP 3);报告所有的错误
error_reporting(E_ALL);
// Same as error_reporting(E_ALL);同上
ini_set(’error_reporting’, E_ALL);
Copy after login

Recommended learning: "PHP Video Tutorial"

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:
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