我检查了我的 PHP ini 文件 (php.ini
) 并设置了 display_errors
并且错误报告是 E_ALL
。我已经重新启动了我的 Apache 网络服务器。
我什至将这些行放在脚本的顶部,它甚至没有捕获简单的解析错误。例如,我使用 "$"
声明变量并且不关闭语句";"
。但是我所有的脚本都在这些错误上显示了一个空白页面,但我希望在浏览器输出中真正看到 errors。
error_reporting(E_ALL);
ini_set('display_errors', 1);
还剩下什么?
$_REQUEST
参数启用它们)的人来说,这两行大部分时间都会起作用。
这总是对我有用:
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
但是,这不会使 PHP 显示解析错误 - 显示这些错误的唯一方法是使用以下行修改 php.ini:
display_errors = on
(如果您无权访问 php.ini
,则将此行放入 .htaccess
也可能有效):
php_flag display_errors 1
在运行时启用错误输出时,您无法捕获解析错误,因为它在实际执行任何操作之前解析文件(并且由于在此期间遇到错误,它不会执行任何操作)。您需要更改实际的服务器配置,以便启用 display_errors 并使用适当的 error_reporting 级别。如果您无权访问 php.ini,则可以使用 .htaccess 或类似文件,具体取决于服务器。
This question 可能会提供其他信息。
在你的 php.ini 里面:
display_errors = on
然后重新启动您的网络服务器。
sudo service apache2 restart
sudo apachectl -k restart
上重新启动。
Loaded Configuration File
ini_get('display_errors')
并返回一个空字符串(意味着它已关闭)。是的,我检查以确保它是配置文件中的唯一行。该设置以某种方式被覆盖,我不知道为什么,这让我发疯。是的,我已经搜索了 /etc/php.d/
中的所有内容,但它也不是这些文件之一。是的,我也重新启动了网络服务器。不,.htaccess
文件中没有任何内容。我正在使用 PHP 7.4.6。
要显示所有错误,您需要:
<强> 1。在您从浏览器(通常是 index.php
)调用的 PHP 脚本中包含这些行:
error_reporting(E_ALL);
ini_set('display_errors', '1');
2.(a) 确保此脚本没有语法错误
-或者-
2.(b) 在您的 php.ini
中设置 display_errors = On
否则,它甚至无法运行那两条线!
您可以通过运行(在命令行)检查脚本中的语法错误:
php -l index.php
如果您包含来自另一个 PHP 脚本的脚本,那么它将在包含的脚本中显示语法错误。例如:
index.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
一些网络托管服务提供商允许您更改 .htaccess
文件中的 PHP 参数。
您可以添加以下行:
php_value display_errors 1
我遇到了和你一样的问题,这个解决方案解决了它。
您可能会发现“错误报告”或“显示错误”的所有设置在 PHP 7 中似乎都不起作用。这是因为错误处理发生了变化。试试这个:
try{
// Your code
}
catch(Error $e) {
$trace = $e->getTrace();
echo $e->getMessage().' in '.$e->getFile().' on line '.$e->getLine().' called from '.$trace[0]['file'].' on line '.$trace[0]['line'];
}
或者,一次性捕获异常和错误(这与 PHP 5 不向后兼容):
try{
// Your code
}
catch(Throwable $e) {
$trace = $e->getTrace();
echo $e->getMessage().' in '.$e->getFile().' on line '.$e->getLine().' called from '.$trace[0]['file'].' on line '.$trace[0]['line'];
}
try{} catch() {}
代码,我什至不想认为这将是一团糟..
$bt = debug_backtrace(); print_r($bt);
。
Error
。如果您将所有请求(可能除了 JavaScript、CSS、图像等)重写到 index.php 文件,然后在其中放置 try catch 块,它会更容易。是的,任何没有单一入口点的系统都将是一个令人头疼的更新。
这将起作用:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
利用:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
这是最好的编写方法,但是语法错误会给出空白输出,因此请使用控制台检查语法错误。调试 PHP 代码的最佳方式是使用控制台;运行以下命令:
php -l phpfilename.php
在你的 index.php 文件中设置它:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
在 PHP 文件所在的文件夹中创建一个名为 php.ini 的文件。
在 php.ini 中添加以下代码(我给出了一个显示代码的简单错误):
display_errors = on
display_startup_errors = on
由于我们现在正在运行 PHP 7,因此这里给出的答案不再正确。唯一还可以的是 the one from Frank Forte,因为他谈到了 PHP 7。
另一方面,与其尝试使用 try/catch 捕获错误,不如使用一个技巧:使用 include。
这里有三段代码:
文件:tst1.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
// Missing " and ;
echo "Testing
?>
在 PHP 7 中运行它不会显示任何内容。
现在,试试这个:
文件:tst2.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
include ("tst3.php");
?>
文件:tst3.php
<?php
// Missing " and ;
echo "Testing
?>
现在运行设置错误报告的 tst2,然后包含 tst3。你会看见:
解析错误:语法错误,文件意外结束,预期变量 (T_VARIABLE) 或 ${ (T_DOLLAR_OPEN_CURLY_BRACES) 或 {$ (T_CURLY_OPEN) 在第 4 行的 tst3.php
我通常会在我的普通 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|E_STRICT);
break;
case 'PRODUCTION':
$base_url = 'Production URL'; /* https://google.com */
error_reporting(0);
/* Mechanism to log errors */
break;
default:
exit('The application environment is not set correctly.');
}
}
如果尽管遵循了上述所有答案(或者您无法编辑 php.ini 文件),您仍然无法收到错误消息,请尝试创建一个启用错误报告的新 PHP 文件,然后包含问题文件。例如:
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once('problem_file.php');
尽管在我的 php.ini
文件中正确设置了所有内容,但这是我可以捕获命名空间错误的唯一方法。我的确切情况是:
//file1.php
namespace a\b;
class x {
...
}
//file2.php
namespace c\d;
use c\d\x; //Dies because it's not sure which 'x' class to use
class x {
...
}
如果您发现自己处于无法通过 php.ini
或 .htaccess
修改设置的情况,那么当您的 PHP 脚本包含解析错误时,您将无法显示错误。然后,您必须像这样解析为 linting the files on the command line:
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.";
}
find . -name '*.php' -type f -exec php -l {} \; | grep -v 'No syntax errors detected'
更简单
您可以执行以下操作:
在主索引文件中设置以下参数:
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);
有关详细信息,请查看 here。
index.html
?
您可以添加自己的自定义错误处理程序,它可以提供额外的调试信息。此外,您可以将其设置为通过电子邮件向您发送信息。
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");
上面的代码应该可以工作:
error_reporting(E_ALL);
但是,尝试在文件中编辑手机上的代码:
error_reporting =on
接受的回答包括额外的选项。在我的 DEVELOPMENT apache vhost 的 PHP 文件中(如果可以确保它不会投入生产,则为 .htaccess):
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
但是,这不会使 PHP 显示解析错误 - 显示这些错误的唯一方法是使用以下行修改 php.ini:
display_errors = on
(如果您无权访问 php.ini
,则将此行放入 .htaccess
也可能有效):
// I've added some extra options that set E_ALL as per https://www.php.net/manual/en/errorfunc.configuration.php.
php_flag log_errors on
php_flag display_errors on
php_flag display_startup_errors on
php_value error_reporting 2147483647
php_value error_log /var/www/mywebsite.ext/logs/php.error.log
如果是快速调试,您可以使用的最佳/简单/快速解决方案是用捕获异常包围您的代码。当我想在生产中快速检查某些东西时,这就是我正在做的事情。
try {
// Page code
}
catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
catch (Throwable $e)
更好...或者 catch(Error $e)
下面的另一个 catch 块
<?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 进行故障排除。
写吧:
error_reporting(-1);
您可以通过更改 php.ini 文件并添加以下内容来做到这一点
display_errors = on
display_startup_errors = on
或者您也可以使用以下代码,因为这对我总是有效
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
如果您安装了 Xdebug,您可以通过以下设置覆盖每个设置:
xdebug.force_display_errors = 1;
xdebug.force_error_reporting = -1;
force_display_errors 类型:int,默认值:0,在 Xdebug >= 2.3 中引入 如果此设置设置为 1,则无论 PHP 的 display_errors 设置如何,错误都会一直显示。 force_error_reporting 类型:int,默认值:0,在 Xdebug >= 2.3 中引入此设置是位掩码,类似于 error_reporting。该位掩码将与 error_reporting 表示的位掩码进行逻辑或运算,以确定应显示哪些错误。此设置只能在 php.ini 中进行,并且允许您强制显示某些错误,无论应用程序使用 ini_set() 做什么。
您可能想使用以下代码:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
报告除 E_NOTICE 之外的所有错误
error_reporting(E_ALL & ~E_NOTICE);
显示所有 PHP 错误
error_reporting(E_ALL); or ini_set('error_reporting', E_ALL);
关闭所有错误报告
error_reporting(0);
如果它在命令行上,您可以使用 -ddisplay_errors=1
运行 php
以覆盖 php.ini
中的设置:
php -ddisplay_errors=1 script.php
error_reporting(1);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
把它放在页面的顶部。
在代码顶部输入这个
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
在 php.ini 文件中,插入:
display_errors = on
这必须有效。
您可以通过简单的方式在显示器中显示 Php 错误。首先,只需将以下代码放入您的 php.ini 文件中。
display_errors = on;
(如果您无权访问 php.ini,那么将这一行放在 .htaccess 中也可能有效):
php_flag display_errors 1
或者您也可以在 index.php 文件中使用以下代码
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
在 Unix CLI 中,重定向 only errors to a file 非常实用:
./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
./script
是一个 php CLI 脚本(hashbang #!/usr/bin/php
)。您可以通过这种方式重定向文件中的 php 错误。这是unix管道。这不是 php 作为 CGI。
不定期副业成功案例分享
php_flag display_errors 1
用于 .htaccessE_ALL
不足以显示 PHP 5.3 中的 all 错误。 “E_STRICT
在 5.4.0 中成为E_ALL
的一部分” - PHP Manual 在该版本中您需要E_ALL | E_STRICT
或-1
。