PHP error_reporting() 函数

本文介绍了PHP中的error_reporting函数,详细解释了如何通过设置不同的级别来控制哪些类型的错误会被报告。文章还提供了示例代码,展示了如何使用该函数来管理运行时错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

定义和用法

error_reporting() 设置 PHP 的报错级别并返回当前级别。

语法

error_reporting(report_level)

如果参数 level 未指定,当前报错级别将被返回。下面几项是 level 可能的值:

常量描述
1E_ERRORFatal run-time errors. Errors that can not be recovered from. Execution of the script is halted
2E_WARNINGNon-fatal run-time errors. Execution of the script is not halted
4E_PARSECompile-time parse errors. Parse errors should only be generated by the parser
8E_NOTICERun-time notices. The script found something that might be an error, but could also happen when running a script normally
16E_CORE_ERRORFatal errors at PHP startup. This is like an E_ERROR in the PHP core
32E_CORE_WARNINGNon-fatal errors at PHP startup. This is like an E_WARNING in the PHP core
64E_COMPILE_ERRORFatal compile-time errors. This is like an E_ERROR generated by the Zend Scripting Engine
128E_COMPILE_WARNINGNon-fatal compile-time errors. This is like an E_WARNING generated by the Zend Scripting Engine
256E_USER_ERRORFatal user-generated error. This is like an E_ERROR set by the programmer using the PHP function trigger_error()
512E_USER_WARNINGNon-fatal user-generated warning. This is like an E_WARNING set by the programmer using the PHP function trigger_error()
1024E_USER_NOTICEUser-generated notice. This is like an E_NOTICE set by the programmer using the PHP function trigger_error()
2048E_STRICTRun-time notices. PHP suggest changes to your code to help interoperability and compatibility of the code
4096E_RECOVERABLE_ERRORCatchable 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)

例子

任意数目的以上选项都可以用“或”来连接(用 OR 或 |),这样可以报告所有需要的各级别错误。例如,下面的代码关闭了用户自定义的错误和警告,执行了某些操作,然后恢复到原始的报错级别:

<?php
//禁用错误报告
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、付费专栏及课程。

余额充值