error_reporting 解释

1E_ERROR

Fatal run-time errors. Errors that can not be recovered from. Execution of the script is halted

 

2E_WARNING

Non-fatal run-time errors. Execution of the script is not halted

 

4E_PARSE

Compile-time parse errors. Parse errors should only be generated by the parser

 

8E_NOTICE

Run-time notices. The script found something that might be an error, but could also happen when running a script normally

 

16E_CORE_ERROR

Fatal errors at PHP startup. This is like an E_ERROR in the PHP core

 

32E_CORE_WARNING

Non-fatal errors at PHP startup. This is like an E_WARNING in the PHP core

 

64E_COMPILE_ERROR

Fatal compile-time errors. This is like an E_ERROR generated by the Zend Scripting Engine

 

128E_COMPILE_WARNING

Non-fatal compile-time errors. This is like an E_WARNING generated by the Zend Scripting Engine

 

256E_USER_ERROR

Fatal user-generated error. This is like an E_ERROR set by the programmer using the PHP function trigger_error()

 

512E_USER_WARNING

Non-fatal user-generated warning. This is like an E_WARNING set by the programmer using the PHP function trigger_error()

 

1024E_USER_NOTICE

User-generated notice. This is like an E_NOTICE set by the programmer using the PHP function trigger_error()

 

2048E_STRICT

Run-time notices. PHP suggest changes to your code to help interoperability and compatibility of the code

 

4096E_RECOVERABLE_ERROR

Catchable fatal error. This is like an E_ERROR but can be caught by a user defined handle (see also set_error_handler())

 

8191E_ALLAll errors and warnings, except level E_STRICT (E_STRICT will be part of E_ALL as of PHP 6.0)

 

 

 

//禁用错误报告
error_reporting(0);

//报告运行时错误
error_reporting(E_ERROR | E_WARNING | E_PARSE);

//报告所有错误
error_reporting(E_ALL);

### PHP `error_reporting` 函数详解 #### 什么是 `error_reporting()`? `error_reporting()` 是 PHP 中的一个内置函数,用于设置运行时的错误报告级别。通过该函数可以动态调整哪些类型的错误会被记录或显示给用户[^1]。 #### 基本语法 以下是 `error_reporting()` 的基本语法: ```php int error_reporting ([ int $level ] ) ``` - **参数**: `$level` 表示要启用的错误报告级别的位掩码组合。 - 如果不传递任何参数,则会返回当前的错误报告级别。 #### 错误报告常量 PHP 提供了一系列预定义的常量来表示不同的错误类型。这些常量可以通过按位 OR (`|`) 组合在一起以指定多个错误类型。常见的错误报告级别包括: - `E_ERROR`: 致命的运行时错误。 - `E_WARNING`: 运行时警告(非致命错误)。 - `E_PARSE`: 解析错误。 - `E_NOTICE`: 运行时通知。 - `E_ALL`: 所有错误和警告。 - `E_STRICT`: 启用 PHP 建议更改的方式以及向后兼容性问题的通知。 - `0`: 关闭所有错误报告[^3]。 #### 示例代码 下面是一些关于如何配置不同错误报告级别的例子: ##### 报告所有可能的错误 如果希望捕获尽可能多的信息以便调试程序,可以选择报告所有错误: ```php <?php error_reporting(E_ALL); ?> ``` ##### 只报告错误而不报告通知 有时开发者只想关注更严重的错误而不是轻微的通知消息: ```php <?php error_reporting(E_ERROR | E_WARNING | E_PARSE); ?> ``` ##### 完全关闭错误报告 当部署到生产环境时,通常会选择隐藏所有错误以免暴露敏感信息: ```php <?php error_reporting(0); // 不再报告任何错误 ini_set('display_errors', '0'); // 确保不会在页面上显示错误 ?> ``` #### 结合其他配置项使用 除了 `error_reporting()` 外,还可以配合 `ini_set()` 来进一步定制行为。例如,在某些情况下不仅需要屏蔽前端可见的错误输出还需要延长执行时间防止因长时间操作引发中断等问题[^2]: ```php <?php // 关闭错误显示 error_reporting(0); ini_set('display_errors', '0'); // 修改最大执行时间为无限期或者适当增加秒数 set_time_limit(0); // 或者 set_time_limit(300) 设定为五分钟后停止 ?> ``` 以上方法能够有效帮助管理应用程序中的异常情况并提升用户体验质量的同时保护服务器安全不受未授权访问威胁影响。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值