; error_reporting is a bit-field. Or each number up to get desired error
; reporting level
; E_ALL - All errors and warnings (doesn't include E_STRICT)
; E_ERROR - fatal run-time errors
; E_RECOVERABLE_ERROR - almost fatal run-time errors
; E_WARNING - run-time warnings (non-fatal errors)
; E_PARSE - compile-time parse errors
; E_NOTICE - run-time notices (these are warnings which often result
; from a bug in your code, but it's possible that it was
; intentional (e.g., using an uninitialized variable and
; relying on the fact it's automatically initialized to an
; empty string)
; E_STRICT - run-time notices, enable to have PHP suggest changes
; to your code which will ensure the best interoperability
; and forward compatibility of your code
; E_CORE_ERROR - fatal errors that occur during PHP's initial startup
; E_CORE_WARNING - warnings (non-fatal errors) that occur during PHP's
; initial startup
; E_COMPILE_ERROR - fatal compile-time errors
; E_COMPILE_WARNING - compile-time warnings (non-fatal errors)
; E_USER_ERROR - user-generated error message
; E_USER_WARNING - user-generated warning message
; E_USER_NOTICE - user-generated notice message
;
; Examples:
;
; - Show all errors, except for notices and coding standards warnings
;
;error_reporting = E_ALL & ~E_NOTICE
;
; - Show all errors, except for notices
;
;error_reporting = E_ALL & ~E_NOTICE | E_STRICT
;
; - Show only errors
;
;error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR
;
; - Show all errors except for notices and coding standards warnings
;
error_reporting = E_ALL & ~E_NOTICE
值得注意的是,即使注册了错误处理函数,默认的行为仍然会执行,也就是错误出现时,仍然会输出错误信息,所以需要在程序中显示的将错误级别设置为0,然后在注册自己的的错误处理函数。这种方式,在生产环境下,尤其重要,因为即时出错,敏感内部错误信息也不会暴露给潜在的恶意用户。还有很重要的一点需要指出,自定义错误处理函数不能处理fatal error(比如编译错误)。下面是一个使用自定义错误处理函数的列子:
<?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_WARNING:
$error_type = 'Warning';
break;
case E_ERROR:
case E_USER_ERROR:
$error_type = 'Fatal Error';
$EXIT = TRUE;
break;
default:
$error_type = 'Unknown';
$EXIT = TRUE;
break;
}
printf ("%s: %s in %s on line %d\n", $error_type, $error_message, $file, $line);
if ($EXIT) {
die();
}
}
set_error_handler ('error_handler');
//new NonExist();
echo $novar;
echo 3/0;
trigger_error ('Trigger a fatal error', E_USER_ERROR);
new NonExist();
?>
执行此脚本可以得到下面的输出:
Notice: Undefined variable: novar in /your/php_demo_file.php on line 40
Warning: Division by zero in /your/php_demo_file.php on line 41
Fatal Error: Trigger a fatal error in /your/php_demo_file.php on line 42