php PHP如何显示错误?(我已经添加了 ini_set 和 error_reporting,但只给出了 500 错误)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10083533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:20:45  来源:igfitidea点击:

How to have PHP display errors? (I've added ini_set and error_reporting, but just gives 500 on errors)

phphtmldebugging

提问by

So, I don't really have any errors in my current web page, but I want to be able to see an error when they pop up, instead of the HTTP 500 error page. I googled around a bit and thought adding these two lines would fix everything.

所以,我当前的网页中没有任何错误,但我希望能够在它们弹出时看到错误,而不是 HTTP 500 错误页面。我用谷歌搜索了一下,认为添加这两行可以解决所有问题。

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

NOTE: I don't have access to the php.ini file, as I'm using my school account's server.

注意:我无权访问 php.ini 文件,因为我使用的是学校帐户的服务器。

So I introduced a bug (no semicolon after $buggy) like so at the top of my page:

所以我在我的页面顶部引入了一个错误($buggy 后没有分号):

<?php 
ini_set('display_errors', 'On');
error_reporting(E_ALL);
$buggy

$x = 4 + 2;
...

However, I just get a Server error:

但是,我只是收到服务器错误:

"The website encountered an error while retrieving http://mywebpage.com/. It may be down for maintenance or configured incorrectly."

“该网站在检索http://mywebpage.com/ 时遇到错误。它可能因维护而关闭或配置不正确。”

Any ideas?

有任何想法吗?

EDIT:

编辑:

I've reconfigured my code:

我重新配置了我的代码:

<?php 
include_once 'database/errorSettings.php';
?>
<?php 

$buggy // whoops
$x = 4 + 2;
...

errorSettings.php is the following:

errorSettings.php 如下:

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

But it still doesn't work... wrong way to reconfigure?

但它仍然不起作用......重新配置的错误方式?

采纳答案by deceze

What you have is a parse error. Those are thrown before any code is executed. A PHP file needs to be parsed in its entirety before any code in it can be executed. If there's a parse error in the file where you're setting your error levels, they won't have taken effect by the time the error is thrown.

你有的是一个解析错误。这些在执行任何代码之前被抛出。PHP 文件需要完整解析,然后才能执行其中的任何代码。如果在您设置错误级别的文件中存在解析错误,则在抛出错误时它们不会生效。

Either break your files up into smaller parts, like setting the error levels in one file and then includeing another file which contains the actual code (and errors), or set the error levels outside PHP using php.ini or .htaccess directives.

要么将您的文件分成更小的部分,例如在一个文件中设置错误级别,然后在include另一个包含实际代码(和错误)的文件中设置错误级别,要么使用 php.ini 或 .htaccess 指令在 PHP 之外设置错误级别。

回答by Nick Clark

You need to set the error_reporting value in a .htaccess file. Since there is a parse error, it never runs the error_reporting() function in your PHP code.

您需要在 .htaccess 文件中设置 error_reporting 值。由于存在解析错误,它永远不会在您的 PHP 代码中运行 error_reporting() 函数。

Try this in a .htaccess file (assuming you can use one):

在 .htaccess 文件中试试这个(假设你可以使用一个):

php_flag display_errors 1
php_value error_reporting 30719

I think 30719 corresponds to E_ALL but I may be wrong.

我认为 30719 对应于 E_ALL 但我可能错了。

Edit Update:http://php.net/manual/en/errorfunc.constants.php

编辑更新:http : //php.net/manual/en/errorfunc.constants.php

int error_reporting ([ int $level ] )
---
32767   E_ALL (integer)     
All errors and warnings, as supported, except of   level E_STRICT prior to PHP 5.4.0.   32767 in PHP 5.4.x, 30719 in PHP 5.3.x, 6143 in PHP   5.2.x, 2047 previously

回答by Matt

Adding to what deceze said above. This is a parse error, so in order to debug a parse error, create a new file in the root named debugSyntax.php. Put this in it:

添加到上面 deceze 所说的内容。这是一个解析错误,因此为了调试解析错误,请在根目录中创建一个名为 debugSyntax.php 的新文件。把这个放进去:

<?php

///////    SYNTAX ERROR CHECK    ////////////
error_reporting(E_ALL);
ini_set('display_errors','On');

//replace "pageToTest.php" with the file path that you want to test. 
include('pageToTest.php'); 

?>

Run the debugSyntax.php page and it will display parse errors from the page that you chose to test.

运行 debugSyntax.php 页面,它将显示您选择测试的页面的解析错误。

回答by Mehul V.

Just write a following code on top of PHP file:

只需在 PHP 文件顶部编写以下代码:

ini_set('display_errors','on');

回答by Paulo Buchsbaum

Syntax errors is not checked easily in external servers, just runtime errors.

在外部服务器中不容易检查语法错误,只是运行时错误。

What I do? Just like you, I use

我所做的?和你一样,我用

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

However, before run I check syntax errors in a PHP file using an online PHP syntax checker.

但是,在运行之前,我使用在线 PHP 语法检查器检查 PHP 文件中的语法错误。

The best, IMHO is PHP Code Checker

最好的,恕我直言是PHP 代码检查器

I copy all the source code, paste inside the main box and click the Analyzebutton.

我复制所有源代码,粘贴到主框内,然后单击Analyze按钮。

It is not the most practical method, but the 2 procedures are complementary and it solves the problem completely

这不是最实用的方法,但2个程序相辅相成,彻底解决问题

回答by Marcelo Agimóvel

To people using Codeigniter (i'm on C3):

对于使用 Codeigniter 的人(我在 C3 上):

The index.php file overwrite php.ini configuration, so on index.php file, line 68:

index.php 文件覆盖了 php.ini 配置,所以 index.php 文件,第 68 行:

case 'development':
        error_reporting(-1);
        ini_set('display_errors', 1);
    break;

You can change this option to set what you need. Here's the complete list:

您可以更改此选项以设置您需要的内容。这是完整的列表:

1   E_ERROR
2   E_WARNING
4   E_PARSE
8   E_NOTICE
16  E_CORE_ERROR
32  E_CORE_WARNING
64  E_COMPILE_ERROR
128 E_COMPILE_WARNING
256 E_USER_ERROR
512 E_USER_WARNING
1024    E_USER_NOTICE
6143    E_ALL
2048    E_STRICT
4096    E_RECOVERABLE_ERROR

Hope it helps.

希望能帮助到你。

回答by Jason

I have had this problem when using PHP5.4 and Plesk 11.5

我在使用 PHP5.4 和 Plesk 11.5 时遇到过这个问题

Somehow, the error reporting and display error settings in the Plesk domain configuration page were completely overriding any local settings in .htaccess or the PHP scripts. I have not found a way to prevent this happening, so use the Plesk settings to turn error reporting on and off.

不知何故,Plesk 域配置页面中的错误报告和显示错误设置完全覆盖了 .htaccess 或 PHP 脚本中的任何本地设置。我还没有找到防止这种情况发生的方法,因此请使用 Plesk 设置来打开和关闭错误报告。

You may have settings in your php.ini that prevents the local site from overriding these settings, perhaps enforced by the control panel used on your server.

您可能在 php.ini 中设置了防止本地站点覆盖这些设置的设置,这可能是由服务器上使用的控制面板强制执行的。