我检查了我的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项目中使用以下代码。
if(!defined('ENVIRONMENT')){
define('ENVIRONMENT', 'DEVELOPMENT');
}
$base_url = null;
if (defined('ENVIRONMENT'))
{
switch (ENVIRONMENT)
{
case 'DEVELOPMENT':
$base_url = 'http://localhost/product/';
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
break;
case 'PRODUCTION':
$base_url = 'Production URL'; /* https://google.com */
error_reporting(E_ALL);
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
ini_set('log_errors', 1); // Mechanism to log errors
break;
default:
exit('The application environment is not set correctly.');
}
}
其他回答
Use:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
这是编写它的最佳方法,但语法错误会产生空白输出,因此请使用控制台检查语法错误。调试PHP代码的最佳方法是使用控制台;运行以下操作:
php -l phpfilename.php
这将起作用:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
为了显示解析错误,可以使用技巧:使用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)
在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
在index.php文件中设置:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);