如何显示PHP错误?

[英]How do I get PHP errors to display?


I have checked my PHP ini file and display errors is set and also error reporting is E_ALL. I have restarted my Apache web server.

我已经检查了我的PHP ini文件,显示错误,并且错误报告是E_ALL。我已经重启了我的Apache web服务器。

I have even put these lines at the top of my script, and it doesn't even catch simple parse errors. For example, I declare variables with a "$" and I don't close statements";". But all my scripts show a blank page on these errors, but I want to actually see the errors in my browser output.

我甚至将这些行放在脚本的顶部,它甚至不能捕获简单的解析错误。例如,我用“$”声明变量,并且不关闭语句“;”但是我的所有脚本都显示了这些错误的空白页,但是我希望看到浏览器输出中的错误。

error_reporting(E_ALL);
ini_set('display_errors', 1);

What is left to do?

剩下要做什么?

23 个解决方案

#1


2314  

This always works for me:

这对我来说总是有效的:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

However, this doesn't make PHP to show parse errors - the only way to show those errors is to modify your php.ini with this line:

但是,这并不能使PHP显示解析错误——显示这些错误的惟一方法是修改您的PHP。ini这条线:

display_errors = on

#2


130  

You can't catch parse errors when enabling error output at runtime, because it parses the file before actually executing anything (and since it encounters an error during this, it won't execute anything). You'll need to change the actual server configuration so that display_errors is on and the approriate error_reporting level is used. If you don't have access to php.ini, you may be able to use .htaccess or similar, depending on the server.

当在运行时启用错误输出时,您无法捕获解析错误,因为它在实际执行任何操作之前解析该文件(并且由于在此期间遇到错误,所以不会执行任何操作)。您需要更改实际的服务器配置,以便使用display_errors,并使用approriate error_reporting级别。如果你无法访问php。ini,您可以使用.htaccess或类似的,取决于服务器。

This question may provide additional info.

这个问题可以提供额外的信息。

#3


113  

Inside your php.ini:

在php . ini中:

display_errors = on

Then restart your web server.

然后重新启动web服务器。

#4


66  

To display all errors you need to:

要显示所有错误,您需要:

1. Have these lines in the PHP script you're calling from the browser (typically index.php):

1。在您从浏览器调用的PHP脚本中有这些行(通常是index.php):

error_reporting(E_ALL);
ini_set('display_errors', 1);

2.(a) Make sure that this script has no syntax errors

2.(a)确保该脚本没有语法错误。

—or—

或者,

2.(b) Set display_errors = On in your php.ini

2.(b)在php.ini中设置display_errors = On。

Otherwise, it can't even run those 2 lines!

否则,它甚至不能运行这两条线!

You can check for syntax errors in your script by running (at the command line):

您可以通过运行(在命令行)来检查脚本中的语法错误:

php -l index.php

If you include the script from another PHP script then it will display syntax errors in the included script. For example:

如果您从另一个PHP脚本中包含脚本,那么它将在包含的脚本中显示语法错误。例如:

index.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

my_script.php

adjfkj // This syntax error will be displayed in the browser

#5


36  

Some web hosting providers allow you to change PHP params in the .htaccess file.

一些虚拟主机提供商允许您在.htaccess文件中更改PHP params。

You can add the following line:

您可以添加以下一行:

php_value display_errors 1

I had the same issue as yours and this solution fixed it.

我有和你一样的问题,这个解决办法解决了。

#6


25  

You might find all of the settings for "error reporting" or "display errors" do not appear to work in PHP 7. That is because error handling has changed. Try this instead:

您可能会发现“错误报告”或“显示错误”的所有设置都不会在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'];
}

Or, to catch exceptions and errors in one go (this is not backward compatible with PHP 5):

或者,在一个go中捕获异常和错误(这不是向后兼容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'];
}

#7


24  

This will work:

这将工作:

<?php
     error_reporting(E_ALL);
     ini_set('display_errors', 1);    
?>

#8


23  

Use:

使用:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

This is the best way to write it, but a syntax error gives blank output, so use the console to check for syntax errors. The best way to debug PHP code is to use the console; run the following:

这是编写它的最好方法,但是语法错误提供了空白输出,因此使用控制台检查语法错误。调试PHP代码的最佳方法是使用控制台;运行以下:

php -l phpfilename.php

#9


13  

Create a file called php.ini in the folder where your PHP file resides.

创建一个名为php的文件。在PHP文件所在的文件夹中。

Inside php.ini add the following code (I am giving an simple error showing code):

在php。ini添加了以下代码(我给出了一个简单的错误显示代码):

display_errors = on

display_startup_errors = on

#10


10  

Here is a PHP script:

这里有一个PHP脚本:

<?php
    ini_set("display_startup_errors", 1);
    ini_set("display_errors", 1);

    /* Reports for either E_ERROR | E_WARNING | E_NOTICE  | Any Error*/
    error_reporting(E_ALL);

    echo(abc); /* Notice: abc is an undefined constant */
?>

For a more detailed explanation of PHP errors, visit PHP Error - error_reporting().

关于PHP错误的更详细的解释,请访问PHP Error - error_reporting()。

#11


9  

When using PHP as an Apache module, we can a change the configuration settings using directives in Apache configuration files (e.g. httpd.conf) and .htaccess files. You will need “AllowOverride Options” or “AllowOverride All” privileges to do so.

当使用PHP作为Apache模块时,我们可以使用Apache配置文件(例如httpd.conf)和.htaccess文件的指令更改配置设置。您将需要“AllowOverride选项”或“AllowOverride All”特权。

Check this

检查这个

http://funbird.co.uk/blog/tech-articals/linux-tech-articals/enabling-error-display-php-via-htaccess

http://funbird.co.uk/blog/tech-articals/linux-tech-articals/enabling-error-display-php-via-htaccess

#12


9  

Set this in your index.php

在index.php中设置这个。

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

#13


8  

If you somehow find yourself in a situation where you can't modifiy the setting via php.ini or .htaccess you're out of luck for displaying errors when your PHP scripts contain parse errors. You'd then have to resolve to linting the files on the command line like this:

如果你在某种情况下发现自己无法通过php修改设置。如果PHP脚本包含解析错误,那么在显示错误时,您就没有运气了。然后,您必须解析在命令行上的文件,如下所示:

find . -name '*.php' -type f -print0 | xargs -0 -n1 -P8 php -l | grep -v "No syntax errors"

If your host is so locked down that it does not allow changing the value via php.ini or .htaccess, it may also disallow changing the value via ini_set. You can check that with the following PHP script:

如果主机被锁定,它不允许通过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.";
}

#14


7  

You can do something like below :

你可以做如下的事情:

Set below parameters in your main index file

在主索引文件中设置以下参数。

    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);

Then based on your requirement you can choose which you want to show :

根据你的要求,你可以选择你想要展示的东西:

For All Error, Warning and Notice

所有错误,警告和通知。

    error_reporting(E_ALL); OR error_reporting(-1);

For All Errors

对所有的错误

    error_reporting(E_ERROR);

For All Warnings

所有的警告

    error_reporting(E_WARNING);

For All Notice

所有通知

    error_reporting(E_NOTICE);

For More Info check here

更多信息请点击这里。

#15


6  

If, despite following all of the above answers (or you can't edit your php.ini file), you still can't get an error message, try making a new PHP file that enables error reporting and then include the problem file. eg:

如果,尽管遵循以上所有的答案(或者你不能编辑你的php)。您仍然不能得到错误消息,尝试创建一个新的PHP文件,该文件允许错误报告,然后包括问题文件。例如:

error_reporting(9999999);
ini_set('display_errors', 1);
require_once('problem_file.php');

Despite having everything set properly in my php.ini file, this was the only way I could catch a namespace error. My exact scenario was:

尽管我在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 {
    ...
}

#16


6  

I would usually go with the following code in my plain php project which are very small, if the project grows large then I will recommend.

我通常会在简单的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   =   'Prod url'; /* https://google.com */
            error_reporting(0);
            /* Mechanism to log errors */
            break;

        default:
            exit('The application environment is not set correctly.');
    }
}

Hope this helps.

希望这个有帮助。

#17


5  

As we are now running PHP7, answers given here are not correct anymore. The only one still OK is the one from Frank Forte, as he talks about PHP7. On the other side, rather than trying to catch error with a try/catch you can use a trick: use include. Here 3 pieces of code:

当我们正在运行PHP7时,这里给出的答案已经不正确了。唯一还可以的是弗兰克福特的,他谈到PHP7。另一方面,与其尝试用try/catch捕获错误,还可以使用一个技巧:使用include。这里有3段代码:

File: tst1.php

文件:tst1.php

<?php
error_reporting(E_ALL);
ini_set('display_errors','On');
// Missing " and ;
echo "Testing
?>  

Running this in PHP7 will show nothing

在PHP7中运行它将不会显示任何东西。

Now, try this:

现在,试试这个:

File: tst2.php

文件:tst2.php

<?php
error_reporting(E_ALL);
ini_set('display_errors','On');
include ("tst3.php");
?> 

File: tst3.php

文件:tst3.php

<?php
// Missing " and ;
echo "Testing
?>  

Now run tst2 which set the error reporting then include tst3. You will see:

现在运行tst2,它设置错误报告,然后包括tst3。你会看到:

Parse error: syntax error, unexpected end of file, expecting variable (T_VARIABLE) or ${ (T_DOLLAR_OPEN_CURLY_BRACES) or {$ (T_CURLY_OPEN) in tst3.php on line 4

解析错误:语法错误、文件的意外结束、预期变量(T_VARIABLE)或${(t_dollar_open_curly_括号)或{$ (T_CURLY_OPEN)在tst3中。php在4号线

#18


4  

You can add your own custom error handler, which can provide extra debug info. Furthermore you can set it up to send you via email.

您可以添加自己的自定义错误处理程序,它可以提供额外的调试信息。此外,你可以设置它通过电子邮件发送给你。

function ERR_HANDLER($errno ,$errstr, $errfile, $errline){
    $msg="<b>Someting 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");

#19


1  

Just write:

只写:

error_reporting(-1);

#20


1  

The best/easy/fast solution that you can use if it's a quick debugging, is to surround your code with catching exceptions. That's what I'm doing when I want to check something fast on production.

如果它是一个快速的调试,您可以使用的最好的/简单的/快速的解决方案是围绕您的代码来捕获异常。这就是我想要在生产中快速检查的时候所做的。

try {

//Page code

} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

I Hope this helps.

我希望这可以帮助。

#21


1  

That's what I learnt. In PHP.INI file

这就是我学到的。在PHP中。INI文件

error_reporting = E_ALL
display_errors = On

#22


1  

This code on top should work error_reporting(E_ALL);

上面的代码应该工作error_reporting(E_ALL);

However try to edit the code on phone in file

但是,尝试在文件中编辑电话代码。

error_reporting =on

error_reporting =对

#23


0  

if you have xdebug installed you can override every setting by setting:

如果您安装了xdebug,您可以通过设置来覆盖所有设置:

xdebug.force_display_errors = 1;
xdebug.force_error_reporting = -1;

force_display_errors

force_display_errors

Type: int, Default value: 0, Introduced in Xdebug >= 2.3 If this setting is set to 1 then errors will always be displayed, no matter what the setting of PHP's display_errors is.

类型:int,默认值:0,在Xdebug >= 2.3中引入,如果设置为1,那么将始终显示错误,无论PHP的display_errors设置是什么。

force_error_reporting

force_error_reporting

Type: int, Default value: 0, Introduced in Xdebug >= 2.3 This setting is a bitmask, like error_reporting. This bitmask will be logically ORed with the bitmask represented by error_reporting to dermine which errors should be displayed. This setting can only be made in php.ini and allows you to force certain errors from being shown no matter what an application does with ini_set().

类型:int,默认值:0,在Xdebug >= 2.3中引入,该设置是位掩码,如error_reporting。这个位掩码将在逻辑上使用由error_reporting表示的位掩码,该位掩码应该显示错误。此设置只能在php中进行。ini允许您强制执行某些错误,无论应用程序如何使用ini_set()。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2009/06/27/87a4be2da8938b4518ba60ff57aecdd8.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号