PHP错误处理函数

来源:互联网 发布:道道通修改端口 编辑:程序博客网 时间:2024/05/17 07:38

debug_backtrace() --  追踪当前代码被调用的源头

PHP debug_backtrace() 函数生成一个 backtrace。

该函数返回一个关联数组。下面是可能返回的元素:

名称类型描述function字符串当前的函数名。line整数当前的行号。file字符串当前的文件名。class字符串当前的类名object对象当前对象。type字符串当前的调用类型,可能的调用:
  • 返回: "->"  - 方法调用
  • 返回: "::"  - 静态方法调用
  • 返回 nothing - 函数调用
args数组如果在函数中,列出函数参数。如果在被引用的文件中,列出被引用的文件名。


function one($str1, $str2) {two("Glenn", "Quagmire");//父父级调用处}function two($str1, $str2) {three("Cleveland", "Brown");//父级调用处}function three($str1, $str2) {print_r(debug_backtrace());//被调用处}one("Peter", "Griffin");


输出结果

Array(
//被调用处
0] => Array(
[file] => E:\wamp\www\a.php
[line] => 8
[function] => three
[args] => Array(
[0] => Cleveland
[1] => Brown
)
)

//父级调用处

[1] => Array(
[file] => E:\wamp\www\a.php
[line] => 4
[function] => two
[args] => Array(
[0] => Glenn
[1] => Quagmire
)
)

//父父级调用处
[2] => Array(
[file] => E:\wamp\www\a.php
[line] => 15
[function] => one
[args] => Array(
[0] => Peter
[1] => Griffin
)
)
)

如果我们想知道某个方法被谁调用了? debug_print_backtrace可以解决
debug_print_backtrace() 

可以打印出一个页面的调用过程 , 从哪儿来到哪儿去一目了然. 
不过这是一个PHP5的专有函数,好在pear中已经有了实现, 

class a {function say($msg) {echo "msg:".$msg;echo "<pre>";debug_print_backtrace();}}class b {function say($msg) {$a = new a();$a->say($msg);}}class c {function __construct($msg) {$b = new b();$b->say($msg);}}$c = new c("test");

结果:

msg:test

#0  a->say(test) called at [E:\wamp\www\a.php:13]#1  b->say(test) called at [E:\wamp\www\a.php:20]#2  c->__construct(test) called at [E:\wamp\www\a.php:24]


error_get_last()获取最后一个报错信息

echo $abc;print_r(error_get_last());

结果:

Array ( [type] => 8 [message] => Undefined variable: abc [file] => E:\wamp\www\a.php [line] => 2 )



error_log()

定义和用法

error_log() 函数向服务器错误记录、文件或远程目标发送一个错误。

若成功,返回 true,否则返回 false。

语法

error_log(error,type,destination,headers)
参数描述error必需。要记录的错误消息。type

可选。规定错误记录的类型。

可能的记录类型:

  • 0 - 默认。根据在 php.ini 文件中的 error_log 配置,错误被发送到服务器日志系统或文件。
  • 1 - 错误被发送到 destination 参数中的地址。只有该类型使用 headers 参数。
  • 2 - 通过 PHP debugging 连接来发送错误。该选项只在 PHP 3 中可用。
  • 3 - 错误发送到文件目标字符串。
destination可选。规定向何处发送错误消息。该参数的值依赖于 "type" 参数的值。headers

可选。只在 "type" 为 1 时使用。

规定附加的头部,比如 From, Cc 以及 Bcc。由 CRLF (\r\n) 分隔。

注释:在发送电子邮件时,必须包含 From 头部。可以在 php.ini 文件中或者通过此参数设置。

例子

本例发送一封带有自定义错误的电子邮件:

<?php$test=2;if ($test>1){error_log("A custom error has been triggered",1,"someone@example.com","From: webmaster@example.com");}?>

输出:

A custom error has been triggered


   error_reporting() 

定义和用法

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 halted2E_WARNINGNon-fatal run-time errors. Execution of the script is not halted4E_PARSECompile-time parse errors. Parse errors should only be generated by the parser8E_NOTICERun-time notices. The script found something that might be an error, but could also happen when running a script normally16E_CORE_ERRORFatal errors at PHP startup. This is like an E_ERROR in the PHP core32E_CORE_WARNINGNon-fatal errors at PHP startup. This is like an E_WARNING in the PHP core64E_COMPILE_ERRORFatal compile-time errors. This is like an E_ERROR generated by the Zend Scripting Engine128E_COMPILE_WARNINGNon-fatal compile-time errors. This is like an E_WARNING generated by the Zend Scripting Engine256E_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 code4096E_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);?>



trigger_error()/user_error()

定义和用法

trigger_error() 函数创建用户定义的错误消息。

trigger_error() 用于在用户指定的条件下触发一个错误消息。它与内建的错误处理器一同使用,也可以与由 set_error_handler() 函数创建的用户自定义函数使用。

如果指定了一个不合法的错误类型,该函数返回 false,否则返回 true。

语法

trigger_error(error_message,error_types)
参数描述error_message必需。规定错误消息。长度限制为 1024 个字符。error_types可选。规定错误消息的错误类型。 可能的值:
  • E_USER_ERROR
  • E_USER_WARNING
  • E_USER_NOTICE

例子

<?php$test=2;if ($test>1){trigger_error("A custom error has been triggered");}?>

输出:

Notice: A custom error has been triggeredin C:\webfolder\test.php on line 6

原创粉丝点击