如何在 PHP 中获取有用的错误消息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/845021/
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
How can I get useful error messages in PHP?
提问by Candidasa
Quite often I will try and run a PHP script and just get a blank screen back. No error message; just an empty screen. The cause might have been a simple syntax error (wrong bracket, missing semicolon), or a failed function call, or something else entirely.
我经常会尝试运行一个 PHP 脚本,然后返回一个空白屏幕。没有错误信息;只是一个空屏幕。原因可能是一个简单的语法错误(错误的括号,缺少分号),或失败的函数调用,或者完全是其他原因。
It is very difficult to figure out what went wrong. I end up commenting out code, entering "echo" statements everywhere, etc. trying to narrow down the problem. But there surely must be a better way, right?
很难弄清楚出了什么问题。我最终注释掉代码,在任何地方输入“echo”语句等,试图缩小问题的范围。但肯定有更好的方法,对吧?
Is there a way to get PHP to produce a useful error message, like Java does?
有没有办法让 PHP 产生有用的错误消息,就像 Java 那样?
采纳答案by Darryl Hein
For syntax errors, you need to enable error display in the php.ini. By default these are turned off because you don't want a "customer" seeing the error messages. Check this pagein the PHP documentation for information on the 2 directives: error_reportingand display_errors. display_errorsis probably the one you want to change. If you can't modify the php.ini, you can also add the following lines to an .htaccess file:
对于语法错误,需要在 php.ini 中开启错误显示。默认情况下,这些是关闭的,因为您不希望“客户”看到错误消息。查看PHP 文档中的此页面以获取有关 2 个指令的信息:error_reporting和display_errors. display_errors可能是你想要改变的那个。如果无法修改 php.ini,还可以在 .htaccess 文件中添加以下几行:
php_flag display_errors on
php_value error_reporting 2039
You may want to consider using the value of E_ALL (as mentioned by Gumbo) for your version of PHP for error_reportingto get all of the errors. more info
您可能需要考虑对您的 PHP 版本使用 E_ALL 的值(如 Gumbo 所述)error_reporting来获取所有错误。更多信息
3 other items: (1) You can check the error log file as it will have all of the errors (unless logging has been disabled). (2) Adding the following 2 lines will help you debug errors that are not syntax errors:
其他 3 项: (1) 您可以检查错误日志文件,因为它将包含所有错误(除非已禁用日志记录)。(2) 添加以下 2 行将帮助您调试不是语法错误的错误:
error_reporting(-1);
ini_set('display_errors', 'On');
(3) Another option is to use an editor that checks for errors when you type, such as PhpEd. PhpEd also comes with a debugger which can provide more detailed information. (The PhpEd debugger is very similar to xdebug and integrates directly into the editor so you use 1 program to do everything.)
(3) 另一种选择是使用编辑器在您键入时检查错误,例如PhpEd。PhpEd 还带有一个调试器,可以提供更详细的信息。(PhpEd 调试器与 xdebug 非常相似,并直接集成到编辑器中,因此您可以使用 1 个程序来完成所有工作。)
Cartman's linkis also very good: http://www.ibm.com/developerworks/library/os-debug/
Cartman 的链接也很好:http: //www.ibm.com/developerworks/library/os-debug/
回答by Eljakim
The following enables all errors:
以下启用所有错误:
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
Also see the following links
另请参阅以下链接
回答by m4dm4x1337
The following code should display all errors:
以下代码应显示所有错误:
<?php
// ----------------------------------------------------------------------------------------------------
// - Display Errors
// ----------------------------------------------------------------------------------------------------
ini_set('display_errors', 'On');
ini_set('html_errors', 0);
// ----------------------------------------------------------------------------------------------------
// - Error Reporting
// ----------------------------------------------------------------------------------------------------
error_reporting(-1);
// ----------------------------------------------------------------------------------------------------
// - Shutdown Handler
// ----------------------------------------------------------------------------------------------------
function ShutdownHandler()
{
if(@is_array($error = @error_get_last()))
{
return(@call_user_func_array('ErrorHandler', $error));
};
return(TRUE);
};
register_shutdown_function('ShutdownHandler');
// ----------------------------------------------------------------------------------------------------
// - Error Handler
// ----------------------------------------------------------------------------------------------------
function ErrorHandler($type, $message, $file, $line)
{
$_ERRORS = Array(
0x0001 => 'E_ERROR',
0x0002 => 'E_WARNING',
0x0004 => 'E_PARSE',
0x0008 => 'E_NOTICE',
0x0010 => 'E_CORE_ERROR',
0x0020 => 'E_CORE_WARNING',
0x0040 => 'E_COMPILE_ERROR',
0x0080 => 'E_COMPILE_WARNING',
0x0100 => 'E_USER_ERROR',
0x0200 => 'E_USER_WARNING',
0x0400 => 'E_USER_NOTICE',
0x0800 => 'E_STRICT',
0x1000 => 'E_RECOVERABLE_ERROR',
0x2000 => 'E_DEPRECATED',
0x4000 => 'E_USER_DEPRECATED'
);
if(!@is_string($name = @array_search($type, @array_flip($_ERRORS))))
{
$name = 'E_UNKNOWN';
};
return(print(@sprintf("%s Error in file \xBB%s\xAB at line %d: %s\n", $name, @basename($file), $line, $message)));
};
$old_error_handler = set_error_handler("ErrorHandler");
// other php code
?>
The only way to generate a blank page with this code is when you have a error in the shutdown handler. I copied and pasted this from my own cms without testing it, but I am sure it works.
使用此代码生成空白页的唯一方法是在关闭处理程序中出现错误时。我从我自己的 cms 复制并粘贴了它,但没有对其进行测试,但我确信它可以工作。
回答by James Anderson
Errors and warnings usually appear in ....\logs\php_error.logor ....\logs\apache_error.logdepending on your php.ini settings.
错误和警告通常出现在....\logs\php_error.log或....\logs\apache_error.log取决于您的 php.ini 设置。
Also useful errors are often directed to the browser, but as they are not valid html they are not displayed.
此外,有用的错误通常会指向浏览器,但由于它们不是有效的 html,因此不会显示。
So "tail -f" your log files and when you get a blank screen use IEs "view" -> "source" menu options to view the raw output.
因此,"tail -f“您的日志文件以及当您获得空白屏幕时,请使用 IE 的“查看”->“源”菜单选项来查看原始输出。
回答by Tomalak
You can include the following lines in the file you want to debug:
您可以在要调试的文件中包含以下几行:
error_reporting(E_ALL);
ini_set('display_errors', '1');
This overrides the default settings in php.ini, which just make PHP report the errors to the log.
这会覆盖 php.ini 中的默认设置,它只会让 PHP 将错误报告给日志。
回答by Madara's Ghost
PHP Configuration
PHP 配置
2 entries in php.inidictate the output of errors:
php.ini 中的2 个条目指示错误的输出:
In production, display_errorsis usually set to Off(Which is a good thing, because error display in production sites is generally not desirable!).
在生产中,display_errors通常设置为Off(这是一件好事,因为生产站点中的错误显示通常是不可取的!)。
However, in development, it should be set to On, so that errors get displayed. Check!
但是,在开发中,它应该设置为On,以便显示错误。检查!
error_reporting(as of PHP 5.3) is set by default to E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED(meaning, everything is shown except for notices, strict standards and deprecation notices). When in doubt, set it to E_ALLto display allthe errors. Check!
error_reporting(自 PHP 5.3 起)默认设置为E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED(意思是,除了通知、严格标准和弃用通知外,所有内容都显示)。如有疑问,请将其设置E_ALL为显示所有错误。检查!
Whoa whoa! No check! I can't change my php.ini!
哇哇!没有检查!我无法更改我的 php.ini!
That's a shame. Usually shared hosts do not allow the alteration of their php.ini file, and so, that option is sadly unavailable. But fear not! We have other options!
这是一种耻辱。通常共享主机不允许更改其 php.ini 文件,因此,遗憾的是该选项不可用。但不要害怕!我们还有其他选择!
Runtime configuration
运行时配置
In the desired script, we can alter the php.ini entries in runtime! Meaning, it'll run when the script runs! Sweet!
在所需的脚本中,我们可以在运行时更改 php.ini 条目!意思是,它会在脚本运行时运行!甜的!
error_reporting(E_ALL);
ini_set("display_errors", "On");
These two lines will do the same effect as altering the php.ini entries as above! Awesome!
这两行代码的作用与上面修改 php.ini 条目的效果相同!惊人的!
I still get a blank page/500 error!
我仍然收到空白页/500 错误!
That means that the script hadn't even run! That usually happens when you have a syntax error!
这意味着脚本甚至没有运行!当您遇到语法错误时,通常会发生这种情况!
With syntax errors, the script doesn't even get to runtime. It fails at compile time, meaning that it'll use the values in php.ini, which if you hadn't changed, may not allow the display of errors.
如果出现语法错误,脚本甚至无法进入运行时。它在编译时失败,这意味着它将使用 php.ini 中的值,如果您没有更改,可能不允许显示错误。
Error logs
错误日志
In addition, PHP by default logs errors. In shared hosting, it may be in a dedicated folder or on the same folder as the offending script.
此外,PHP 默认会记录错误。在共享主机中,它可能位于专用文件夹中或与违规脚本位于同一文件夹中。
If you have access to php.ini, you can find it under the error_logentry.
如果您有权访问 php.ini,则可以在error_log条目下找到它。
回答by gnarf
回答by FDisk
I'm always using this syntax at the very top of the php script.
我总是在 php 脚本的最顶部使用这种语法。
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 'On'); //On or Off
回答by Eduardo Oliveira
It is possible to register an hook to make the last error or warning visible.
可以注册一个钩子来使最后一个错误或警告可见。
function shutdown(){
var_dump(error_get_last());
}
register_shutdown_function('shutdown');
adding this code to the beginning of you index.php will help you debug the problems.
将此代码添加到 index.php 的开头将帮助您调试问题。
回答by hakre
For quick, hands-on troubleshooting I normally suggest here on SO:
为了快速、动手进行故障排除,我通常建议在 SO 上:
error_reporting(~0); ini_set('display_errors', 1);
to be put at the beginning of the script that is under trouble-shooting. This is not perfect, the perfect variant is that you also enable that in the php.iniand that you log the errors in PHP to catch syntax and startup errors.
放在正在排除故障的脚本的开头。这并不完美,完美的变体是您还启用它php.ini并在 PHP 中记录错误以捕获语法和启动错误。
The settings outlined here display all errors, notices and warnings, including strict ones, regardless which PHP version.
此处概述的设置显示所有错误、通知和警告,包括严格的错误、通知和警告,无论 PHP 版本如何。
Next things to consider:
接下来要考虑的事情:
- Install Xdebugand enable remote-debugging with your IDE.
- 安装Xdebug并使用您的 IDE 启用远程调试。
See as well:
另见:

