Home >Backend Development >PHP Problem >How to set up php.ini error reporting

How to set up php.ini error reporting

藏色散人
藏色散人Original
2021-03-05 17:58:183294browse

How to set php.ini error reporting: First find and open the php.ini configuration file; then set the content to "error_reporting=E_ALL display_errors=On".

How to set up php.ini error reporting

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

PHP Turn on error display and set error reporting level

Warning: Never display any error messages in a production environment!

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.

Commonly used settings in projects

switch (ENVIRONMENT)
{
    // 对于开发环境,报告所有错误,同时显示到屏幕上
    case 'development':
        error_reporting(-1);
        ini_set('display_errors', 1);
    break;

    // 对于测试和生产环境,不显示错误,5.3 以上的版本,不报告通知、废弃方法、严格这几类错误
    case 'testing':
    case 'production':
        ini_set('display_errors', 0);
        if (version_compare(PHP_VERSION, '5.3', '>='))
        {
            error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED);
        }
        else
        {
            error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_USER_NOTICE);
        }
    break;

    default:
        header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
        echo 'The application environment is not set correctly.';
        exit(1); // EXIT_ERROR
}

PHP predefined error constants

The official website defines all error 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.

Enable and set the display level in code

ini_set() function

For PHP, you can pass php.ini file sets various instructions. 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 set the error_reporting directive at runtime. 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 # 显示错误

[Recommended learning: "PHP Video Tutorial"]

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