PHP错误处理 - “错误的ErrorHandler参数”

时间:2012-07-07 11:55:12

标签: php function error-handling

我正在尝试第一次设置错误处理 它实际上工作并报告错误(如果有),但由于某种原因,它总是显示错误处理函数本身的“缺少参数”的错误。请注意我的错误处理函数是在一个单独的文件中并包含在索引页面中,我不确定这是否是问题所在:S

这是我的错误处理功能

function errorHandler($errno, $errstr, $error_file, $error_line) {

  if(isset($errstr)) {
    # There is an error, display it
    echo $errno." - ".$errstr." in ".$error_file." at line ".$error_line."<br>";
  } else {
    # There isn't any error, do nothing
    return false;
  }

}

// We must tell PHP to use the above error handler.
set_error_handler("errorHanlder");

这是索引页面   

<!-- # Error Handler -->
<? if(errorHandler()) { ?>
<section id="error-handler">
  <?=errorHandler();?>
</section>
<? } ?>

以下是浏览器中的结果(请记住没有php错误,所以这个错误处理程序不应该输出任何东西 - 这是我无法理解的

2 - Missing argument 1 for errorHandler(), called in index.php on line 20 and defined in inc/arcError.fnc.php at line 10
2 - Missing argument 2 for errorHandler(), called in index.php on line 20 and defined in inc/arcError.fnc.php at line 10
2 - Missing argument 3 for errorHandler(), called in index.php on line 20 and defined in inc/arcError.fnc.php at line 10
2 - Missing argument 4 for errorHandler(), called in index.php on line 20 and defined in inc/arcError.fnc.php at line 10

任何想法都是为什么PHP会报告缺少的参数?

1 个答案:

答案 0 :(得分:7)

您正在使用ZERO参数调用该函数...

<?=errorHandler();?>

为什么你还需要打电话呢?

你的代码中有几个拼写错误:替换&#34; Hanlder&#34;使用&#34; Handler&#34;。

没有必要这样做:

if(isset($errstr)) {

出现错误时会自动调用错误处理函数(仅在这种情况下!)。 $ errstr是此函数的一个参数,它总是在函数执行时设置。

新代码:

function errorHandler($errno, $errstr, $error_file, $error_line) {
    # There is an error, display it
    echo "<section id='error-handler'>$errno - $errstr in $error_file at line $error_line</section><br>";
}

// We must tell PHP to use the above error handler.
set_error_handler("errorHandler");