Home >Backend Development >PHP Tutorial >PHP error handling in development mode and product mode

PHP error handling in development mode and product mode

不言
不言Original
2018-04-20 11:45:221581browse

This article introduces you to PHP error handling in development mode and product mode through example code. It is very good and has reference value. Friends who need it can refer to it.

Program error reporting is always there. It's inevitable, even though we take extra care when writing the 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'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 built-in functions 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 ]]] )
Sends an error message to the web server's error log or to a file.

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


The above is the detailed content of PHP error handling in development mode and product mode. 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