Home  >  Article  >  Backend Development  >  Detailed explanation of PHP error handling in development mode and product mode

Detailed explanation of PHP error handling in development mode and product mode

藏色散人
藏色散人forward
2020-02-03 16:13:582611browse

Program errors are always inevitable, even though we have been extra careful when writing code.

When developing php programs, we hope that when encountering php errors, they can be displayed to us as soon as possible to facilitate debugging. When the program is developed and becomes a formal product, we hope to record unexpected error messages in the error log instead of displaying these error messages to users, because users are very likely to use these to expose script paths, database information or Other error messages indicate destructive hacking operations.

PHP error handling

If the php script encounters an error during execution, it will be handled in the form of an error. Some errors will terminate the script and continue execution after the error is reported. Some won't, please refer to the manual for details.

php's error handling method is based on the following configuration options. These configurations can be declared in the code or set in the php.ini file. If you do not need to change these configurations frequently, it is recommended to set them in the php.ini file to make your code cleaner and more concise.

# 是否打印错误信息到浏览器/命令行界面
# 开发模式下建议开启,产品模式下强烈建议关闭
ini_set('display_errors', 'On');
# 是否记录错误信息到日志
# 开发模式和产品模式下都建议开启
ini_set('log_errors', 'On');
# 指定错误信息日志文件,若开启了 log_errors 选项,记得指定日志文件位置
# 要确保执行 php 脚本的系统用户拥有该文件的 write 权限,否则日志无法被写入
ini_set('error_log', '/usr/local/php/errors.log');
# 该选项用以设定错误报告的等级
# 等同于 error_reporting(E_ALL) 
# 无论开发模式还是产品模式下都建议开到E_ALL(报告所有的错误信息)
# 产品模式下也需要设置此选项,因为关闭了 display_errors 并开启了 log_errors
# 所以浏览器/命令行界面不会因此暴露报错信息
ini_set('error_reporting', E_ALL);

In addition, PHP also provides developers with a built-in function to record customized error information to the error log file in the code:

bool error_log ( string $message [, int $message_type = 0 [, string $destination [, string $extra_headers ]]] )

The required parameter is message, Calling this function will write the message to the error_log file defined in php.ini.

User-defined error handling

In addition, users can handle script running in a customized way through the function:

mixed set_error_handler ( callable $error_handler [, int $error_types = E_ALL | E_STRICT ] )

Errors that occur when the user registers an error_handler and specifies error_types, then when errors of these error_types occur, PHP's standard error handler

will be bypassed (that is, no error message will be output, The error message will not be logged), but the handler in error_handler will be executed.

For detailed usage of this function, please refer to the manual (https://www.php.net/manual/zh/function.set-error-handler.php)

Recommended related article tutorials: php tutorial

The above is the detailed content of Detailed explanation of PHP error handling in development mode and product mode. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete