我经常会尝试运行PHP脚本,然后得到一个空白屏幕。没有错误信息;只有一个空屏幕。原因可能是一个简单的语法错误(错误的括号,缺少分号),或者函数调用失败,或者完全是其他原因。

很难找出哪里出了问题。我最终注释掉了代码,到处输入“echo”语句,等等,试图缩小问题的范围。但肯定有更好的办法,对吧?

是否有一种方法可以让PHP像Java那样产生有用的错误消息?


当前回答

“错误”是开发人员了解他们的错误并解决它们以使系统完美工作的最有用的东西。

PHP提供了一些更好的方法来了解开发人员为什么以及他们的代码在哪里出错,因此通过了解这些错误,开发人员可以在许多方面改进他们的代码。

最好的方法是在脚本顶部写以下两行来获得所有错误消息:

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

在IDE中使用xdebug等调试器工具的另一种方法。

其他回答

如果错误是在PHP代码中,您可以在代码中使用error_reporting()函数来设置为报告所有。

但是,这并不能处理PHP崩溃的情况。这方面的信息只能在服务器日志中找到。也许你无法访问这些,但我合作过的许多托管提供商都有某种方式让你访问它。例如,我最喜欢的方法是在.php所在的当前目录中创建error_log文件。尝试在那里搜索或联系您的主机提供商。

error_reporting(E_ALL | E_STRICT);

并在php.ini中打开显示错误

我不知道它是否有帮助,但这里是我的php项目的标准配置文件的一部分。我倾向于不太依赖apache配置,即使是在我自己的服务器上。

我从来没有遇到过错误消失的问题,所以也许这里的一些东西会给你一个想法。

编辑以显示APPLICATON_LIVE

/*
APPLICATION_LIVE will be used in process to tell if we are in a development or production environment.  It's generally set as early as possible (often the first code to run), before any config, url routing, etc.
*/

if ( preg_match( "%^(www.)?livedomain.com$%", $_SERVER["HTTP_HOST"]) ) {
    define('APPLICATION_LIVE', true);
} elseif ( preg_match( "%^(www.)?devdomain.net$%", $_SERVER["HTTP_HOST"]) ) {
    define('APPLICATION_LIVE', false);
} else {
    die("INVALID HOST REQUEST (".$_SERVER["HTTP_HOST"].")");
    // Log or take other appropriate action.
}


/*
--------------------------------------------------------------------
DEFAULT ERROR HANDLING
--------------------------------------------------------------------
Default error logging.  Some of these may be changed later based on APPLICATION_LIVE.
*/
error_reporting(E_ALL & ~E_STRICT);
ini_set ( "display_errors", "0");
ini_set ( "display_startup_errors", "0");
ini_set ( "log_errors", 1);
ini_set ( "log_errors_max_len", 0);
ini_set ( "error_log", APPLICATION_ROOT."logs/php_error_log.txt");
ini_set ( "display_errors", "0");
ini_set ( "display_startup_errors", "0");

if ( ! APPLICATION_LIVE ) {
    // A few changes to error handling for development.
    // We will want errors to be visible during development.
    ini_set ( "display_errors", "1");
    ini_set ( "display_startup_errors", "1");
    ini_set ( "html_errors", "1");
    ini_set ( "docref_root", "http://www.php.net/");
    ini_set ( "error_prepend_string", "<div style='color:red; font-family:verdana; border:1px solid red; padding:5px;'>");
    ini_set ( "error_append_string", "</div>");
}

打开错误报告是正确的解决方案,但是它似乎不会在打开它的程序中生效,而只会在随后包含的程序中生效。

因此,我总是创建一个文件/程序(我通常称之为“genwrap.php”),它的代码本质上与这里流行的解决方案相同。打开错误报告),然后它还包括我实际想要调用的页面。

实现这个调试有两个步骤;

首先,创建genwrap.php,然后把下面的代码放进去:

<?php
error_reporting(-1);
ini_set('display_errors', 'On');

include($_REQUEST['page']);
?>

第二,将你想要调试的程序/页面的链接改为通过genwrap.php,

例如:改变:

$.ajax('dir/pgm.php?param=val').done(function(data) { /* ... */

to

$.ajax('dir/genwrap.php?page=pgm.php&param=val').done(function(data) { /* ... */

您可以启用完整的错误报告(包括通知和严格消息)。有些人觉得这太啰嗦了,但值得一试。在php.ini中设置error_reporting为E_ALL | E_STRICT。

error_reporting = E_ALL | E_STRICT

E_STRICT将通知您已弃用的函数,并为您提供关于执行某些任务的最佳方法的建议。

如果你不想要通知,但你发现其他消息类型有帮助,尝试排除通知:

error_reporting = (E_ALL | E_STRICT) & ~E_NOTICE

还要确保在php.ini中启用了display_errors。如果您的PHP版本高于5.2.4,请将其设置为On:

display_errors = "On"

如果您的版本是5.2.4或更新版本,请使用:

display_errors = "stderr"