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

How to modify the error level in php

Jun 02, 2021 pm 06:38 PM
php error 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] )
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!

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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles