为什么使用set_error_handler()压制我的错误?

时间:2012-05-18 20:18:41

标签: php

我正在使用以下脚本处理错误:

<?php # config.inc.php

// This script establishes email default settings.
// This script determines how errors are handled.

// Email Settings
$site['from_name'] = 'x'; // from email name
$site['from_email'] = 'x@x.com'; // from email address

// Just in case we need to relay to a different server, 
// provide an option to use external mail server.
$site['smtp_mode'] = 'enabled'; // enabled or disabled
$site['smtp_host'] = 'mail.x.com';
$site['smtp_port'] = null;
$site['smtp_username'] = 'admin@x.com'; 
$site['smtp_password'] = 'x';

// Error handling:
// Flag variable for site status:
$live = TRUE;

ini_set('display_errors','On');
error_reporting(E_ALL);

// Error log email address:
$admin_email = 'x@x.com';

date_default_timezone_set('America/New_York');

// Create the error handler.
function my_error_handler ($e_number, $e_message, $e_file, $e_line, $e_vars) {

    global $live, $admin_email;

    // Build the error message.
    $message = "An error occurred in script '$e_file' on line $e_line: \n<br />$e_message\n<br />";

    // Add the date and time.
    $message .= "Date/Time: " . date('n-j-Y H:i:s') . "\n<br />";

    // Append $e_vars to the $message.
    $message .= "<pre>" . print_r ($e_vars, 1) . "</pre>\n<br />";

    if ($live) { // Don't show the specific error.

        error_log ($message, 1, $admin_email); // Send email.

        // Only print an error message if the error isn't a notice.
        if ($e_number != E_NOTICE) {
            echo '<div id="Error">A system error occurred. An administrator has been notified. We apologize for the inconvenience.</div><br />';
        }

    } else { // Development (print the error).
        echo '<div id="Error">' . $message . '</div><br />';
    }

} // End of my_error_handler() definition.

// Use my error handler.
set_error_handler ('my_error_handler');

?>

如果我在带有错误的脚本中包含此脚本,则不会显示错误。如果我注释掉对set_error_handler()的调用,则会显示错误。我做错了什么可能导致这种行为?

1 个答案:

答案 0 :(得分:1)

来自文档http://php.net/manual/en/function.set-error-handler.php

  

重要的是要记住标准的PHP错误处理程序是   完全绕过error_types指定的错误类型   除非回调函数返回FALSE。

让您的处理程序返回FALSE