写给愿意动手的 PHP 开发者
PHP学习网 PHP学习网
公告PHP学习网为公益性网站,致力于为广大PHP爱好者服务
首页/ PHP 函数大全/ 运行环境/ set_error_handler()
运行环境 PHP PHP 4 set_error_handler

set_error_handler()

注册自定义错误处理函数,可以把警告转成异常。

set_error_handler(?callable $callback, int $error_levels = E_ALL): ?callable

参数

参数类型说明
$callback?callable处理函数,接收级别、消息、文件、行号
$error_levelsint要接管的错误级别

返回值

?callable — 返回之前的处理函数。

示例

PHP
set_error_handler(function ($no, $str, $file, $line) {
    if (!(error_reporting() & $no)) return false;
    throw new ErrorException($str, 0, $no, $file, $line);
});

注意

  • 返回 false 表示交回 PHP 默认处理。
  • 它接管不了 Fatal Error 和 Parse Error,那两类要用 register_shutdown_function 兜。