如何显示PHP错误?
我已经检查了我的PHP ini文件(如何显示PHP错误?,php,error-handling,syntax-error,error-reporting,Php,Error Handling,Syntax Error,Error Reporting,我已经检查了我的PHP ini文件(PHP.ini),设置了display\u errors,并且错误报告是E\u ALL。我已经重新启动了Apache Web服务器 我甚至把这些行放在了脚本的顶部,它甚至没有捕捉到简单的解析错误。例如,我使用“$”声明变量,并且不关闭语句“;”。但是我所有的脚本都会在这些错误上显示一个空白页,但我希望在浏览器输出中实际看到错误 error_reporting(E_ALL); ini_set('display_errors', 1); 剩下要做什么?在运行时启
PHP.ini
),设置了display\u errors
,并且错误报告是E\u ALL
。我已经重新启动了Apache Web服务器
我甚至把这些行放在了脚本的顶部,它甚至没有捕捉到简单的解析错误。例如,我使用“$”
声明变量,并且不关闭语句“;”
。但是我所有的脚本都会在这些错误上显示一个空白页,但我希望在浏览器输出中实际看到错误
error_reporting(E_ALL);
ini_set('display_errors', 1);
剩下要做什么?在运行时启用错误输出时,您无法捕获解析错误,因为它在实际执行任何操作之前解析文件(由于在此过程中遇到错误,因此不会执行任何操作)。您需要更改实际的服务器配置,以便启用“显示错误”,并使用适当的错误报告级别。如果您没有访问php.ini的权限,则可能可以使用.htaccess或类似工具,具体取决于服务器
可以提供其他信息。在您的php.ini中:
display_errors = on
然后重新启动您的web服务器。一些web托管提供商允许您更改
.htaccess
文件中的PHP参数
您可以添加以下行:
php_value display_errors 1
我的问题与您的问题相同,此解决方案解决了此问题。要显示所有错误,您需要: 1。在从浏览器调用的PHP脚本中包含以下行(通常为
index.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);
2.(a)确保此脚本没有语法错误
-或-
2.(b)在php.ini中设置display\u errors=On
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';
adjfkj // This syntax error will be displayed in the browser
否则,它甚至不能运行这两条线
您可以通过运行(在命令行)检查脚本中的语法错误:
如果包含来自另一个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';
adjfkj // This syntax error will be displayed in the browser
my_script.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';
adjfkj // This syntax error will be displayed in the browser
这对我来说总是有用的:
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
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
在php文件所在的文件夹中创建一个名为php.ini的文件
在php.ini内部添加以下代码(我给出了一个显示代码的简单错误):
如果,尽管遵循了上述所有答案(或者您无法编辑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脚本包含解析错误时,您就很难显示错误。然后,您必须下定决心像这样:
find . -name '*.php' -type f -print0 | xargs -0 -n1 -P8 php -l | grep -v "No syntax errors"
如果主机被锁定,不允许通过php.ini
或.htaccess
更改值,则也可能不允许通过ini\u set
更改值。您可以使用以下PHP脚本进行检查:
<?php
if( !ini_set( 'display_errors', 1 ) ) {
echo "display_errors cannot be set.";
} else {
echo "changing display_errors via script is possible.";
}
您可能会发现“错误报告”或“显示错误”的所有设置在PHP7中似乎都不起作用。这是因为错误处理已更改。请尝试以下方法:
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'];
}
或者,要一次性捕获异常和错误(这与PHP5不向后兼容):
使用:
这是编写它的最佳方式,但语法错误会产生空白输出,因此请使用控制台检查语法错误。调试PHP代码的最佳方法是使用控制台;运行以下命令:
php -l phpfilename.php
只要写下:
error_reporting(-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|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.');
}
}
您可以执行以下操作:
在主索引文件中设置以下参数:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
然后根据您的要求,您可以选择要显示的内容:
对于所有错误、警告和通知:
error_reporting(E_ALL); OR error_reporting(-1);
对于所有错误:
对于所有警告:
error_reporting(E_WARNING);
所有通知:
有关更多信息,请查看。如果是快速调试,您可以使用的最佳/简单/快速解决方案是在代码周围添加捕获异常。当我想在生产中快速检查某些东西时,我就是这么做的
try {
// Page code
}
catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
上面的代码应该可以工作:
error_reporting(E_ALL);
但是,请尝试在文件中编辑手机上的代码:
error_reporting =on
由于我们现在正在运行PHP7,这里给出的答案不再正确。当他谈到PHP7时,唯一还行的是
另一方面,您可以使用一个技巧:使用include,而不是尝试通过try/catch捕捉错误
这里有三段代码:
文件:tst1.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
// Missing " and ;
echo "Testing
?>
在PHP7中运行此命令将不会显示任何内容
现在,试试这个:
文件:tst2.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
include ("tst3.php");
?>
文件:tst3.php
<?php
// Missing " and ;
echo "Testing
?>
现在运行设置错误报告的tst2,然后包括tst3。你会看到:
解析错误:语法错误,文件意外结束,第4行的tst3.php中应为变量(T_variable)或${(T_DOLLAR_OPEN_CURLY_大括号)或{$(T_CURLY_OPEN)
您可以添加自己的自定义错误处理程序,它可以提供额外的调试信息。此外,您可以将其设置为通过电子邮件向您发送信息
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");
函数ERR\u处理程序($errno、$errstr、$errfile、$errline){
$msg=“发生了不好的事情。[$errno]$errstr
文件:$errfile
行:$errline
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
.json_encode(debug_backtrace(),json_PRETTY_PRINT)。“
”;
echo$msg;
返回false;
}
函数EXC_处理程序($exception){
错误处理程序(0,$exception->getMessage(),$exception->getFile(),$exception->getLine
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");
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
xdebug.force_display_errors = 1;
xdebug.force_error_reporting = -1;
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
display_errors = on
display_startup_errors = on
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
./script 2> errors.log
fwrite(STDERR, "Debug infos\n"); // Write in errors.log^
tail -f errors.log
watch cat errors.log
error_reporting(E_ALL & ~E_NOTICE);
error_reporting(E_ALL); or ini_set('error_reporting', E_ALL);
error_reporting(0);
display_errors = on;
php_flag display_errors 1
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
error_reporting(1);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);