我检查了我的PHP ini文件(PHP.ini),并设置了display_errors,错误报告也是E_ALL。我已重新启动Apache Web服务器。
我甚至把这些行放在了脚本的顶部,它甚至不能捕捉到简单的解析错误。例如,我用“$”声明变量,而不关闭语句“;”。但我的所有脚本都显示了这些错误的空白页面,但我希望在浏览器输出中看到这些错误。
error_reporting(E_ALL);
ini_set('display_errors', 1);
还有什么要做?
我检查了我的PHP ini文件(PHP.ini),并设置了display_errors,错误报告也是E_ALL。我已重新启动Apache Web服务器。
我甚至把这些行放在了脚本的顶部,它甚至不能捕捉到简单的解析错误。例如,我用“$”声明变量,而不关闭语句“;”。但我的所有脚本都显示了这些错误的空白页面,但我希望在浏览器输出中看到这些错误。
error_reporting(E_ALL);
ini_set('display_errors', 1);
还有什么要做?
当前回答
<?php
// Turn off error reporting
error_reporting(0);
// Report runtime errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// Report all errors
error_reporting(E_ALL);
// Same as error_reporting(E_ALL);
ini_set("error_reporting", E_ALL);
// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);
?>
当您的站点处于活动状态时,出于安全原因,php.ini文件应该禁用display_errors。但是,对于开发环境,可以启用display_errors进行故障排除。
其他回答
您可以添加自己的自定义错误处理程序,它可以提供额外的调试信息。此外,您可以将其设置为通过电子邮件向您发送信息。
function ERR_HANDLER($errno, $errstr, $errfile, $errline){
$msg = "<b>Something bad happened.</b> [$errno] $errstr <br><br>
<b>File:</b> $errfile <br>
<b>Line:</b> $errline <br>
<pre>".json_encode(debug_backtrace(), JSON_PRETTY_PRINT)."</pre> <br>";
echo $msg;
return false;
}
function EXC_HANDLER($exception){
ERR_HANDLER(0, $exception->getMessage(), $exception->getFile(), $exception->getLine());
}
function shutDownFunction() {
$error = error_get_last();
if ($error["type"] == 1) {
ERR_HANDLER($error["type"], $error["message"], $error["file"], $error["line"]);
}
}
set_error_handler ("ERR_HANDLER", E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED);
register_shutdown_function("shutdownFunction");
set_exception_handler("EXC_HANDLER");
在Unix CLI中,只将错误重定向到文件是非常实用的:
./script 2> errors.log
在脚本中,可以像往常一样使用var_dump()或等效方法(STDOUT和STDERR都将接收输出),但只能在日志文件中写入:
fwrite(STDERR, "Debug infos\n"); // Write in errors.log^
然后从另一个shell中进行实时更改:
tail -f errors.log
或者干脆
watch cat errors.log
为了显示解析错误,可以使用技巧:使用include,而不是在php.ini中设置display_errors。
以下是三段代码:
文件:tst1.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
// Missing " and ;
echo "Testing
当直接运行此文件时,如果php.ini中将display_errors设置为0,它将不显示任何内容。
现在,试试这个:
文件:tst2.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
include ("tst3.php");
文件:tst3.php
<?php
// Missing " and ;
echo "Testing
现在运行设置错误报告的tst2.php,然后包含tst3。您将看到:
分析错误:语法错误,意外的文件结尾,第4行tst3.php中需要变量(T_variable)或${(T_DOLLAR_OPEN_CURLY_BRACES)或{$(T_CURLY_OPEN)
要显示所有错误,您需要:
1.在从浏览器(通常是index.PHP)调用的PHP脚本中包含以下行:
error_reporting(E_ALL);
ini_set('display_errors', '1');
2.(a)确保此脚本没有语法错误
—or—
2.(b)在php.ini中设置display_errors=打开
否则,它甚至无法运行这两条线!
您可以通过运行(在命令行)检查脚本中的语法错误:
php -l index.php
如果包含另一个PHP脚本中的脚本,那么它将在包含的脚本中显示语法错误。例如:
索引php
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Any syntax errors here will result in a blank screen in the browser
include 'my_script.php';
my_script.php
adjfkj // This syntax error will be displayed in the browser
如果是在命令行上,则可以使用-display_errors=1运行php以覆盖php.ini中的设置:
php -ddisplay_errors=1 script.php