Home > Backend Development > PHP Tutorial > php set error level

php set error level

王林
Release: 2023-04-07 13:18:01
Original
5977 people have browsed it

php set error level

php error level setting

You can set the server’s alarm level for errors in php.ini. 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 ​​for level:

php set error level

In the php.ini file, the default report is all errors except notifications, as follows Statement setting:

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 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 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 to 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.

Error reporting for PHP
There are many configuration settings in the php.ini file. You should have set up your php.ini file and placed it in the appropriate directory, as shown in the documentation for installing PHP and Apache2 on Linux. 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: The default value of the

display_errors = On
Copy after login

error_reporting variable is E_ALL. This setting will show everything from bad coding practices to harmless tips to errors. E_ALL is a bit too fine-grained for development because it also displays hints on the screen for small things (such as variables not being initialized), which messes up the browser's output. I only want to see errors and bad coding practices, but not harmless tips. 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 difficult to tell which PHP version Apache is using because Apache can only look at one php.ini file. Not knowing which php.ini file Apache is using to configure 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 a good idea 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 is 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 the php.ini file for display_errors already The flag to set, and the value of error_reporting. The 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 PHP The file compilation failed because the writing format did not follow PHP's encoding rules and caused errors that cannot be blocked.

The 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 tutorial:PHP video tutorial

The above is the detailed content of php set error level. 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