13.1 错误处理
语法错误
运行时错误
逻辑错误
13.1.1 错误报告级别
如果开发人员希望在PHP脚本中将错误消息报告给他,则必须在配置文件php.ini中,将display_errors指令设置为On,开启PHP输出醋无报告的功能。
13.1.2 跳帧错误报告级别
可以通过在配置文件php.ini中,修改配置指令error_reporting的值,修改成功后重新启动Web服务器,则每个PHP脚本都可以按调整后的错误级别输出错误报告。
或者可以在PHP脚本中使用error_reporting()函数,基于各个脚本来调整这种行为。
error_reporting(0); //设置为0会全部关闭错误报告
error_reporting(E_ALL); //将会向PHP报告发生的每个错误
error_reporting(E_ALL & ~E_NOTICE); //可以抛出任何非注意的错误报告
PHP脚本只有在遇到错误时才会终止运行,输出的错误报告结果。“注意”和“警告”的错误报告并不会终止程序运行。如果不希望有注意和警告的报告输出,就可以在脚本error.php中修改error_reporting()函数
error_reporting(E_ALL&~(E_WARNING|E_NOTICE)) //报告除注意和警告之外的所有错误
除了使用error_reporting和display_error两个配置指令可以秀嘎嘎icuowu报告行为之外,还有许多配置指令可以确定PHP的错误行为。
13.1.3 使用trigger_error()函数来替代die()
函数die()等同于exit(),trigger_error()则可以生成一个用户警告来代替,使程序更具有灵活性。
trigger_error("没有找到文件",EUSER_ERROR);
13.1.4 自定义错误处理
以下几种情况可以考虑自定义错误处理:
可以记下错误的信息,及时发现一些生产环境出现的问题
可以用来屏蔽错误
可以做相应的处理
可以作为调试工具
通常使用set_error_handler()函数去设置用户自定义的错误处理函数,该函数用于创建运行时期间的用户自己的错误处理方法,返回旧的错误处理程序,若失败,则返回Null。
<?php
error_reporting(0);
function error_handler($error_level,$error_message,$file,$line){
$EXIT=FALSE;
switch($error_level){
case E_NOTICE:
case E_USER_NOTICE:
$error_type="Notice";break;
case E_WARNING:
case E_USER_ERROR:
$error_type="Waring";break;
case E_ERROR:
case E_USER_ERROR:
$error_type="Fatal Error";
$EXIT=TRUE;break;
default:
$error_type="Unknown";
$EXIT=TRUE;break;
}
printf("<font color='#FF0000'><b>%s</b></font>: %s in <b>%s</b> on line <b>%d</b><br>\n",$error_level);
if(TRUE==$EXIT){
echo '<script>location="err.html";</script>';
}
}
set_error_handler('error_handler');
echo $novar;
echo 3/0;
trigger_error("Trigger a fatal error",E_USER_ERROR);
?>
mixed set_error_handler ( callable $error_handler [, int $error_types = E_ALL | E_STRICT ] )
参数
error_handler
用户的函数需要接受两个参数:错误码和描述错误的 string。 另外有可能提供三个可选参数:发生错误的文件名、发生错误的行号 以及发生错误的上下文(一个指向错误发生时活动符号表的 array)。 该函数可以表示为:
handler ( int $errno , string $errstr [, string $errfile [, int $errline [, array $errcontext ]]] )
errno
第一个参数 errno,包含了错误的级别,是一个 integer。
errstr
第二个参数 errstr,包含了错误的信息,是一个 string。
errfile
第三个参数是可选的,errfile, 包含了发生错误的文件名,是一个 string。
errline
第四个参数是一个可选项, errline, 包含了错误发生的行号,是一个 integer。
errcontext
第五个可选参数, errcontext, 是一个指向错误发生时活动符号表的 array。 也就是说,errcontext 会包含错误触发处作用域内所有变量的数组。 用户的错误处理程序不应该修改错误上下文(context)。
如果函数返回 FALSE,标准错误处理处理程序将会继续调用。
error_types
就像error_reporting 的 ini 设置能够控制错误的显示一样, 此参数能够用于屏蔽 error_handler 的触发。 如果没有该掩码, 无论 error_reporting 是如何设置的, error_handler 都会在每个错误发生时被调用。
返回值
如果之前有定义过错误处理程序,则返回该程序名称的 string;如果是内置的错误处理程序,则返回 NULL。 如果你指定了一个无效的回调函数,同样会返回 NULL。 如果之前的错误处理程序是一个类的方法,此函数会返回一个带类和方法名的索引数组(indexed array)。
<?php
// error handler function
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
if (!(error_reporting() & $errno)) {
// This error code is not included in error_reporting
return;
}
switch ($errno) {
case E_USER_ERROR:
echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
echo " Fatal error on line $errline in file $errfile";
echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
echo "Aborting...<br />\n";
exit(1);
break;
case E_USER_WARNING:
echo "<b>My WARNING</b> [$errno] $errstr<br />\n";
break;
case E_USER_NOTICE:
echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";
break;
default:
echo "Unknown error type: [$errno] $errstr<br />\n";
break;
}
/* Don't execute PHP internal error handler */
return true;
}
// function to test the error handling
function scale_by_log($vect, $scale)
{
if (!is_numeric($scale) || $scale <= 0) {
trigger_error("log(x) for x <= 0 is undefined, you used: scale = $scale", E_USER_ERROR);
}
if (!is_array($vect)) {
trigger_error("Incorrect input vector, array of values expected", E_USER_WARNING);
return null;
}
$temp = array();
foreach($vect as $pos => $value) {
if (!is_numeric($value)) {
trigger_error("Value at position $pos is not a number, using 0 (zero)", E_USER_NOTICE);
$value = 0;
}
$temp[$pos] = log($scale) * $value;
}
return $temp;
}
// set to the user defined error handler
$old_error_handler = set_error_handler("myErrorHandler");
// trigger some errors, first define a mixed array with a non-numeric item
echo "vector a\n";
$a = array(2, 3, "foo", 5.5, 43.3, 21.11);
print_r($a);
// now generate second array
echo "----\nvector b - a notice (b = log(PI) * a)\n";
/* Value at position $pos is not a number, using 0 (zero) */
$b = scale_by_log($a, M_PI);
print_r($b);
// this is trouble, we pass a string instead of an array
echo "----\nvector c - a warning\n";
/* Incorrect input vector, array of values expected */
$c = scale_by_log("not array", 2.3);
var_dump($c); // NULL
// this is a critical error, log of zero or negative number is undefined
echo "----\nvector d - fatal error\n";
/* log(x) for x <= 0 is undefined, you used: scale = $scale" */
$d = scale_by_log($a, -2.5);
var_dump($d); // Never reached
?>
以上例程的输出类似于:
vector a
Array
(
[0] => 2
[1] => 3
[2] => foo
[3] => 5.5
[4] => 43.3
[5] => 21.11
)
----
vector b - a notice (b = log(PI) * a)
<b>My NOTICE</b> [1024] Value at position 2 is not a number, using 0 (zero)<br />
Array
(
[0] => 2.2894597716988
[1] => 3.4341896575482
[2] => 0
[3] => 6.2960143721717
[4] => 49.566804057279
[5] => 24.165247890281
)
----
vector c - a warning
<b>My WARNING</b> [512] Incorrect input vector, array of values expected<br />
NULL
----
vector d - fatal error
<b>My ERROR</b> [256] log(x) for x <= 0 is undefined, you used: scale = -2.5<br />
Fatal error on line 35 in file trigger_error.php, PHP 5.2.1 (FreeBSD)<br />
Aborting...<br />
13.1.5 写错误日志
1. 使用指定的文件记录错误报告日志
如果使用自己指定的文件记录错误日志,一定要确保将这个文件存放在文档根目录之外,以减少遭到攻击的可能。并且该文件一定要让PHP脚本的执行用户具有读写权限。
error_reporting =E_ALL ;将会向PHP报告发生的每个错误
display_errors=Off ;不显示满足上条指令所定义规则的所有错误报告
log_errors=On ;决定日志语句记录的位置
log_errors_max_len=1024 ;设置每个日志项的最大长度
error_log=/usr/local/error.log ;指定产生的错误报告写入的日志文件位置
bool error_log ( string $message [, int $message_type = 0 [, string $destination [, string $extra_headers ]]] )
<?php if(!Ora_logon($username,$password)){ error_log("Oracle数据库不可用!",0); } if(!($foo=allocate_new_foo())){ error_log("出现大麻烦了!",1,"webmaster@www.mydomain.com"); } error_log("搞砸了!",2,"loacalhost:5000"); error_log("搞砸了!",3,"/usr/local/errors.log"); ?>
2. 错误信息记录到操作系统的日志里
如果希望将错误报告写到操作系统的日志里,可以在配置文件中将error_log指令的值设置为syslog。
error_reporting=E_ALL ;将会向PHP报告发生的每个错误
display_errors=Off ;不显示满足上条指令所定义规则的所有错误报告
log_errors=On ;决定日志语句记录的位置
log_errors_max_len=1024 ;设置每个日志项的最大长度
error_log=syslog ;指定产生的错误报告写入操作系统的日志里
define_syslog_variables()
openlog()
syslog()
closelog()
13.2 异常处理
13.2.1 异常处理实现
try{
......
}catch(ex1){
......
}
在PHP代码中所产生的异常可以被throw语句抛出并被catch语句捕获。需要进行异常处理的代码都必须放入try代码内,以便捕获可能存在的异常。
<?php
try{
$error='Always throw this error';
throw new Exception($error);
echo 'Never executed';
} catch(Exception $e){
echo 'Caught exception:',$e->getMessage(),"\n";
}
echo 'Hello World';
?>