php 报错处理函数,PHP错误处理函数

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 "

";

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 文件中或者通过此参数设置。

例子

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

$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 可能的值:

常量

描述

1

E_ERROR

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

2

E_WARNING

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

4

E_PARSE

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

8

E_NOTICE

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

16

E_CORE_ERROR

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

32

E_CORE_WARNING

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

64

E_COMPILE_ERROR

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

128

E_COMPILE_WARNING

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

256

E_USER_ERROR

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

512

E_USER_WARNING

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

1024

E_USER_NOTICE

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

2048

E_STRICT

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

4096

E_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())

8191

E_ALL

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

例子

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

//禁用错误报告

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

例子

$test=2;

if ($test>1)

{

}

?>trigger_error("A custom error has been triggered");

输出:

Notice: A custom error has been triggered

in C:\webfolder\test.php on line 6

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值