Home  >  Article  >  Backend Development  >  How to display error level in php

How to display error level in php

青灯夜游
青灯夜游Original
2021-05-31 18:28:492145browse

How to display the error level in php: first find and open the php.ini configuration file; then set the value of the "error_reporting" item to "E_ALL", set the value of the "display_errors" item to "On"; finally save Just file.

How to display error level in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Warning: Never use in production environment Show any error messages!

Display errors (display_errors) and error reporting (error_reporting) are two different things. When an error occurs in a PHP script, you can choose whether to report the error (record it in the error log) according to the settings. If display_errors is turned on in the settings, the error message will be printed to the screen at the same time.

Enable and set the display level in code

ini_set() function

For PHP , various instructions can be set through the php.ini file. But sometimes you need to set instructions when the script is running, then you need the ini_set() function.

string ini_set ( string $varname , string $newvalue )

Set the value of the specified configuration option. This option will retain its new value while the script is running, and will be restored when the script ends.

For example:

ini_set('error_reporting', E_ALL);
ini_set('display_errors', 'on');

error_reporting() function

error_reporting() The function can be run When setting the error_reporting directive. PHP has many error levels. Use this function to set the level when the script is running. If the optional argument is not set, error_reporting() returns the current error reporting level.

The default value for PHP7.2 is E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED.

It is recommended to enable E_NOTICE during the development stage to display more possible errors.

<?php

// 关闭所有PHP错误报告,相当于 ini_set(&#39;error_reporting&#39;, 0);
error_reporting(0);

error_reporting(E_ERROR | E_WARNING | E_PARSE);

// 报告 E_NOTICE  (报告未初始化的变量或捕获变量名的错误拼写)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// 除了 E_NOTICE,报告其他所有错误
error_reporting(E_ALL ^ E_NOTICE);

// 报告所有 PHP 错误 (参见 changelog)
error_reporting(E_ALL);

// 报告所有 PHP 错误
error_reporting(-1);

// 和 error_reporting(E_ALL); 一样
ini_set(&#39;error_reporting&#39;, E_ALL);

Modify the php.ini configuration file

error_reporting = E_ALL # 报告所有错误
display_errors = On # 显示错误

PHP predefined error constants

The official website defines all errors Constants, commonly used ones are:

  • E_STRICT (integer) PHP suggestions for code modifications to ensure the best interoperability and forward compatibility of the code.
  • E_ALL (integer) All error and warning information except E_STRICT.
  • E_ERROR (integer) Fatal runtime error. This type of error is generally an unrecoverable situation, such as a problem caused by memory allocation. The consequence is that the script terminates and does not continue to run.
  • E_WARNING (integer) Runtime warning (non-fatal error). Only a prompt message is given, but the script does not terminate.
  • E_PARSE (integer) Compile-time syntax parsing error. Parsing errors are generated only by the parser.
  • E_NOTICE (integer) Runtime notification. Indicates that the script encounters a situation that may appear as an error, but there may also be similar notifications in scripts that can run normally.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to display 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