0

我第一次尝试设置错误处理。
如果有错误,它确实可以工作并报告错误,但由于某种原因,它总是显示错误处理函数本身的“缺少参数”错误。请注意我的错误处理函数在一个单独的文件中,并且包含在索引页面中,我不确定这是否是问题: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 会报告缺少的参数吗?

4

1 回答 1

7

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

<?=errorHandler();?>

为什么你仍然需要调用它?

您的代码中有几个拼写错误:将“Hanlder”替换为“Handler”。

没有必要这样做:

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");
于 2012-07-07T11:58:09.250 回答