我检查了我的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);
还有什么要做?
当前回答
Use:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
这是编写它的最佳方法,但语法错误会产生空白输出,因此请使用控制台检查语法错误。调试PHP代码的最佳方法是使用控制台;运行以下操作:
php -l phpfilename.php
其他回答
您可以执行以下操作:
在主索引文件中设置以下参数:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
然后根据您的要求,您可以选择要显示的内容:
对于所有错误、警告和通知:
error_reporting(E_ALL); OR error_reporting(-1);
对于所有错误:
error_reporting(E_ERROR);
对于所有警告:
error_reporting(E_WARNING);
对于所有通知:
error_reporting(E_NOTICE);
有关详细信息,请查看此处。
如果您发现自己处于无法通过php.ini或.htaccess修改设置的情况下,那么当您的php脚本包含解析错误时,您就不可能显示错误。然后,您必须解决以下问题:
find . -name '*.php' -type f -print0 | xargs -0 -n1 -P8 php -l | grep -v "No syntax errors"
如果您的主机被锁定,不允许通过php.ini或.htaccess更改值,那么它也可能不允许通过ini_set更改值。您可以使用以下PHP脚本进行检查:
<?php
if( !ini_set( 'display_errors', 1 ) ) {
echo "display_errors cannot be set.";
} else {
echo "changing display_errors via script is possible.";
}
如果安装了Xdebug,则可以通过以下设置覆盖每个设置:
xdebug.force_display_errors = 1;
xdebug.force_error_reporting = -1;
强制显示错误类型:int,默认值:0,在Xdebug中引入>=2.3如果设置设置为1,则无论PHP的display_errors的设置是什么。强制错误报告类型:int,默认值:0,在Xdebug中引入>=2.3此设置是位掩码,如error_reporting。此位掩码将与error_reporting表示的位掩码进行逻辑“或”运算,以将错误显示到dermine。此设置只能在php.ini中进行,允许您强制显示某些错误,无论应用程序如何使用ini_set()。
在index.php文件中设置:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
由于不清楚您使用的是什么操作系统,这些是我的2美分Windows。如果您正在使用XAMPP,则需要在C:\xamp.php下手动创建日志文件夹。不是你的错,ApacheFriends提交了这个。
要阅读并遵循此文件,请执行以下操作。
Get-Content c:\xampp\php\logs\php_error_log -Wait
要在VSCode中执行此操作,请在.VSCode\tasks.json中创建一个任务
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Monitor php errors",
"type": "shell",
"command": "Get-Content -Wait c:\\xampp\\php\\logs\\php_error_log",
"runOptions": {
"runOn": "folderOpen"
}
}
]
并在加载文件夹时运行。