Home >Backend Development >PHP Problem >How to use php error reporting function

How to use php error reporting function

藏色散人
藏色散人Original
2022-10-25 11:01:211868browse

How to use the php error reporting function: 1. Use the "error_reporting()" function directly in the program to set the php error reporting level; 2. Find and open the "php.ini" file, and then configure the parameters to Just control the error message.

How to use php error reporting function

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

How to use the php error reporting function?

PHP error_reporting() Detailed explanation of error control function function

Definition and usage:

error_reporting() Sets the error level of PHP and returns Current level.

Function syntax:

error_reporting(report_level)

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

值 常量 描述
1 E_ERROR 致命的运行错误。错误无法恢复,暂停执行脚本。
2 E_WARNING 运行时警告(非致命性错误)。非致命的运行错误,脚本执行不会停止。
4 E_PARSE 编译时解析错误。解析错误只由分析器产生。
8 E_NOTICE 运行时提醒(这些经常是你代码中的bug引起的,也可能是有意的行为造成的。)
16 E_CORE_ERROR PHP启动时初始化过程中的致命错误。
32 E_CORE_WARNING PHP启动时初始化过程中的警告(非致命性错)。
64 E_COMPILE_ERROR 编译时致命性错。这就像由Zend脚本引擎生成了一个E_ERROR。
128 E_COMPILE_WARNING 编译时警告(非致命性错)。这就像由Zend脚本引擎生成了一个E_WARNING警告。
256 E_USER_ERROR 用户自定义的错误消息。这就像由使用PHP函数trigger_error(程序员设置E_ERROR)
512 E_USER_WARNING 用户自定义的警告消息。这就像由使用PHP函数trigger_error(程序员设定的一个E_WARNING警告)本贴由FastMVC首发,谢谢关注FastMVC。
1024 E_USER_NOTICE 用户自定义的提醒消息。这就像一个由使用PHP函数trigger_error(程序员一个E_NOTICE集)
2048 E_STRICT 编码标准化警告。允许PHP建议如何修改代码以确保最佳的互操作性向前兼容性。
4096 E_RECOVERABLE_ERROR 开捕致命错误。这就像一个E_ERROR,但可以通过用户定义的处理捕获(又见set_error_handler())
8191 E_ALL 所有的错误和警告(不包括 E_STRICT) (E_STRICT will be part of E_ALL as of PHP 6.0)

Example:

Any number of the above options can be connected with "OR" (using OR or |), so that all required Errors at all levels.

For example, the following code turns off user-defined errors and warnings, performs certain operations, and then returns to the original error level:

//禁用错误报告
error_reporting(0);
//报告运行时错误
error_reporting(E_ERROR | E_WARNING | E_PARSE);
//报告所有错误
error_reporting(E_ALL);
?>

php turns on and off errors Tip

There are several ways to turn on and off error prompts in php. One can directly use the relevant functions in the program to open an account. The other we can use the configuration parameters in php.ini to control , let me introduce it to you students.

Windows system switch php error message

If you do not have the permission to modify php.ini, you can add the following code to the php file:

ini_set("display_errors", "On");
error_reporting(E_ALL | E_STRICT);

Of course, if you can modify php .ini, as follows:

Find display_errors = On and change it to display_errors = off

Note: If you have copied the PHP.ini file to the windows directory, you must also change c : display_errors = On in windows/php.ini is changed to display_errors = off

Solution to the failure of display_errors = Off in PHP .ini

The methods of turning on and off error prompts in Linux systems are similar. However, I will give you a detailed introduction

Linux system

1. Open the php.ini file.

Take my ubuntu as an example. This file is in the /etc/php5/apache2 directory.

2. Search and modify the following line, change the Off value to On

display_errors = Off

3. Search for the following line

error_reporting = E_ALL & ~E_NOTICE

or search:

error_reporting = E_ALL & ~E_DEPRECATED

and change it to

error_reporting = E_ALL | E_STRICT

Code

4. Modify Apache’s httpd.conf,

Take my Ubuntu as an example, this file is in the /etc/apache2/ directory, this is Blank document.

Add the following two lines:

php_flag display_errors on
php_value error_reporting 2039

5. Restart Apache and it will be OK.

Restart command

sudo /etc/init.d/apache2 restart

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to use php error reporting function. 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