Cla*_*key 0 php error-handling function
我正在尝试第一次设置错误处理.
它实际上工作并报告错误(如果有),但由于某种原因,它总是显示错误处理函数本身的"缺少参数"的错误.请注意我的错误处理函数是在一个单独的文件中并包含在索引页面中,我不确定这是否是问题: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");
Run Code Online (Sandbox Code Playgroud)
这是索引页面
<!-- # Error Handler -->
<? if(errorHandler()) { ?>
<section id="error-handler">
<?=errorHandler();?>
</section>
<? } ?>
Run Code Online (Sandbox Code Playgroud)
这是浏览器中的结果(请记住没有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
Run Code Online (Sandbox Code Playgroud)
任何想法都是为什么PHP报告缺少的参数?
你用ZERO参数调用函数...
<?=errorHandler();?>
Run Code Online (Sandbox Code Playgroud)
你为什么还要打电话呢?
你的代码中有几个拼写错误:将"Hanlder"替换为"Handler".
没有必要这样做:
if(isset($errstr)) {
Run Code Online (Sandbox Code Playgroud)
出现错误时会自动调用错误处理函数(仅在这种情况下!).$ 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");
Run Code Online (Sandbox Code Playgroud)