Home >Backend Development >PHP Problem >How to set up php.ini error reporting
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".
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:
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('error_reporting', 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('error_reporting', 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!