Setting method of php error level

In our program development, we often encounter some program running errors. PHP will give different prompts according to the error level of the program. For example, what warnings, errors and other errors, but these errors can be set, if it is just some small errors, we can prevent these errors from being reported. This requires how to set the PHP error level explained in this chapter.

In PHP, there are two ways to set the error level:

第一种: After setting error_reporting in this way, restart the web server and it will take effect permanently.

Taking the xampp integrated software package as an example, open the configuration file php.ini and check the default value of the error report level error_reporting, as follows:

error_reporting=E_ALL & ~E_DEPRECATED & ~E_STRICT

It means to report all errors, except for E_DEPRECATED and E_STRICT.

Modify it to:

error_reporting=E_ALL & ~E_NOTICE

It means to report all errors, except for E_NOTICE. This is also the most commonly used error reporting level. It does not report errors of attention classes (such as the use of undefined variables).

Save and take effect after restarting the web server.

第二种: Set the error reporting level through the error_reporting() function. After this method is set, it can take effect immediately. But only in the area after the error_reporting() function call in the current script.

The syntax of the error_reporting() function is as follows:

int error_reporting ([ int $level ] )

The parameter level is the level of the specified error. If it is not set, the current error level will be returned. Below is the value of the level parameter.

value constant Description
1 E_ERROR Report a fatal error that caused the script to terminate
2 E_WARNING Report warning errors during runtime (the script will not terminate the operation)
4 E_PARSE Report compile-time parsing errors
8 E_NOTICE Report notification errors, scripts may produce errors
16 E_CORE_ERROR Fatal error during PHP startup during initialization.
32 E_CORE_WARNING Warnings (non-fatal errors) during the initialization process when PHP starts.
64 E_COMPILE_ERROR Fatal error at compile time. This is like an E_ERROR generated by the Zend script engine.
128 E_COMPILE_WARNING Compile-time warnings (non-fatal errors). This is like an E_WARNING warning generated by the Zend script engine.
256 E_USER_ERROR User-defined warning message. This is like using the PHP function trigger_error (an E_WARNING warning set by the programmer)
512 E_USER_WARNING User-defined warning message. This is like using the PHP function trigger_error (an E_WARNING warning set by the programmer)
1024 E_USER_NOTICE User-defined warning message. This is like using the PHP function trigger_error (an E_WARNING warning set by the programmer)
2048 E_STRICT Code standardization warning. Allow PHP to suggest how to modify the code to ensure the best interoperability and forward compatibility.
4096 E_RECOVERABLE_ERROR A fatal error was opened. This is like an E_ERROR, but can be caught by user-defined processing (see also set_error_handler())
8191 E_ALL All errors and warnings (excluding E_STRICT) (E_STRICT will be part of E_ALL as of PHP 6.0)
Any number of the above options can be connected by "or" (using OR or ), so you can report all the required errors of each level.

example:

The following code turns off user-defined errors and warnings, performs some operations, and then restores to the original error level:

//禁用错误报告

error_reporting(0);

//报告运行时错误

error_reporting(E_ERROR | E_WARNING | E_PARSE);

//报告所有错误

error_reporting(E_ALL);

A simple example is:

error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

He said that php errors, warnings, syntax errors, and reminders all return errors.

Reprint address: https://www.php.cn/php-weizijiaocheng-361195.html

Guess you like

Origin blog.csdn.net/baidu_33055905/article/details/114652730