search
HomeBackend DevelopmentPHP ProblemHow to modify the error level in php

How to modify the error level in php

Jun 02, 2021 pm 06:38 PM
phperror level

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] )

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        报告不赞成的用法和不推荐的行为

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)

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)

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 核心错误警告。

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

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

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

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

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);

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!

Statement
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
ACID vs BASE Database: Differences and when to use each.ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PM

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

PHP Secure File Uploads: Preventing file-related vulnerabilities.PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

PHP Input Validation: Best practices.PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PM

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

PHP API Rate Limiting: Implementation strategies.PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PM

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

PHP Password Hashing: password_hash and password_verify.PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PM

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP XSS Prevention: How to protect against XSS.PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PM

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

PHP Interface vs Abstract Class: When to use each.PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PM

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version