set_error_handler,如何在php中抑制E_NOTICE

时间:2014-02-14 09:09:43

标签: php suppress e-notices

如何在此php函数中禁止E_NOTICE,我不想从生产站点获取有关非声明变量的电子邮件?

我试图发表评论案例E_NOTICE:但是程序只是跳到默认值:退出(“$ errfile中的未知错误:$ errline”);靠近剧本的末尾。

我希望有机会评论出我不想要的错误反馈吗?

<?php

function errorHandler($errno, $errstr, $errfile, $errline) {

    function errorMail($errno, $errstr, $errfile, $errline) {

        $recvmail = "yourmail";

    $fejlkode = array(
        1 => "E_ERROR",
        2 => "E_WARNING",
        4 => "E_PARSE",
        8 => "E_NOTICE",
        16 => "E_CORE_ERROR",
        32 => "E_CORE_WARNING",
        64 => "E_COMPILE_ERROR",
        128 => "E_COMPILE_WARNING",
        256 => "E_USER_ERROR",
        512 => "E_USER_WARNING",
        1024 => "E_USER_NOTICE",
        2048 => "E_STRICT",
        4096 => "E_RECOVERABLE_ERROR",
        8192 => "E_DEPRECATED",
        16384 => "E_USER_DEPRECATED",

    );

    $mail = new PHPMailer;
        $mail->From = 'mailSender';
        $mail->FromName = 'your firm name';
        $mail->AddAddress($recvmail, "your name");  // Add a recipient
        $mail->SetLanguage('dk', './');
        $mail->Subject = utf8_decode("Fejl type:" . $errno .' -> '. $fejlkode[$errno]);
        $mail->Body    = utf8_decode($errstr.' <br>'.$errfile.': '.$errline);
        $mail->AltBody = $errstr.$errfile.$errline.$errno;

        if(!$mail->Send()) {
          echo 'Message could not be sent.';
          echo 'Mailer Error: ' . $mail->ErrorInfo;
          exit;
       }  
}

switch ($errno) {
    case E_NOTICE:
    case E_USER_NOTICE:
    case E_DEPRECATED:
    case E_USER_DEPRECATED:
    case E_STRICT:

        errorMail($errno, $errstr, $errfile, $errline);

        break;

    case E_WARNING:
    case E_USER_WARNING:
       errorMail($errno, $errstr, $errfile, $errline);
        break;

    case E_ERROR:
    case E_USER_ERROR:
       errorMail($errno, $errstr, $errfile, $errline);
        exit("FATAL error $errstr at $errfile:$errline");

    default:

        exit("Unknown error at $errfile:$errline");
    }
}

set_error_handler("errorHandler");
?>

3 个答案:

答案 0 :(得分:1)

break

之后添加case E_NOTICE语句
case E_NOTICE:
    break;
case E_USER_NOTICE:
case E_DEPRECATED:
case E_USER_DEPRECATED:
case E_STRICT:
    ...

答案 1 :(得分:1)

switch ($errno) {
    case E_NOTICE:
        break; // Do nothing

    case E_USER_NOTICE:
    case E_DEPRECATED:
    case E_USER_DEPRECATED:
    case E_STRICT:

        errorMail($errno, $errstr, $errfile, $errline);

        break;

    case E_WARNING:
    case E_USER_WARNING:
       errorMail($errno, $errstr, $errfile, $errline);
        break;

    case E_ERROR:
    case E_USER_ERROR:
       errorMail($errno, $errstr, $errfile, $errline);
        exit("FATAL error $errstr at $errfile:$errline");

    default:

        exit("Unknown error at $errfile:$errline");
    }
}

答案 2 :(得分:1)

将其放在页面顶部:

function serious_log_errors($number, $message, $file, $line, $vars)  
{  
if ( ($number !== E_NOTICE) && ($number < 2048) ) 
 { 
$forlogfile = "A serious error ($number) occurred on line $line and in the file: $file.   $message.\r\n";  
error_log($forlogfile, 3, 'errors.php'); 
 }  
}

set_error_handler('serious_log_errors'); 

set_error_handler告诉它在出现错误时使用什么功能。该函数只会将严重错误(而不是通知)记录到名为errors.php

的文件中