我经常会尝试运行PHP脚本,然后得到一个空白屏幕。没有错误信息;只有一个空屏幕。原因可能是一个简单的语法错误(错误的括号,缺少分号),或者函数调用失败,或者完全是其他原因。
很难找出哪里出了问题。我最终注释掉了代码,到处输入“echo”语句,等等,试图缩小问题的范围。但肯定有更好的办法,对吧?
是否有一种方法可以让PHP像Java那样产生有用的错误消息?
我经常会尝试运行PHP脚本,然后得到一个空白屏幕。没有错误信息;只有一个空屏幕。原因可能是一个简单的语法错误(错误的括号,缺少分号),或者函数调用失败,或者完全是其他原因。
很难找出哪里出了问题。我最终注释掉了代码,到处输入“echo”语句,等等,试图缩小问题的范围。但肯定有更好的办法,对吧?
是否有一种方法可以让PHP像Java那样产生有用的错误消息?
当前回答
这个答案是由冗余部门为您带来的。
ini_set() / php.ini / .htaccess / .user.ini The settings display_errors and error_reporting have been covered sufficiently now. But just to recap when to use which option: ini_set() and error_reporting() apply for runtime errors only. php.ini should primarily be edited for development setups. (Webserver and CLI version often have different php.ini's) .htaccess flags only work for dated setups (Find a new hoster! Well managed servers are cheaper.) .user.ini are partial php.ini's for modern setups (FCGI/FPM) And as crude alternative for runtime errors you can often use: set_error_handler("var_dump"); // ignores error_reporting and `@` suppression error_get_last() Can be used to retrieve the last runtime notice/warning/error, when error_display is disabled. $php_errormsg Is a superlocal variable, which also contains the last PHP runtime message. isset() begone! I know this will displease a lot of folks, but isset and empty should not be used by newcomers. You can add the notice suppression after you verified your code is working. But never before. A lot of the "something doesn't work" questions we get lately are the result of typos like: if(isset($_POST['sumbit'])) # ↑↑ You won't get any useful notices if your code is littered with isset/empty/array_keys_exists. It's sometimes more sensible to use @, so notices and warnings go to the logs at least. assert_options(ASSERT_ACTIVE|ASSERT_WARNING); To get warnings for assert() sections. (Pretty uncommon, but more proficient code might contain some.) PHP7 requires zend.assertions=1 in the php.ini as well. declare(strict_types=1); Bending PHP into a strictly typed language is not going to fix a whole lot of logic errors, but it's definitely an option for debugging purposes. PDO / MySQLi And @Phil already mentioned PDO/MySQLi error reporting options. Similar options exist for other database APIs of course. json_last_error() + json_last_error_msg For JSON parsing. preg_last_error() For regexen. CURLOPT_VERBOSE To debug curl requests, you need CURLOPT_VERBOSE at the very least. shell/exec() Likewise will shell command execution not yield errors on its own. You always need 2>&1 and peek at the $errno.
其他回答
PHP配置
php.ini中的2个条目指示错误的输出:
display_errors error_reporting
在生产中,display_errors通常设置为Off(这是一件好事,因为在生产站点中错误显示通常是不可取的!)
但是,在开发中,应该将其设置为On,以便显示错误。检查!
error_reporting(从PHP 5.3开始)默认设置为E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED(意思是,除了通知、严格标准和弃用通知之外的所有内容都会显示)。当不确定时,将其设置为E_ALL以显示所有错误。检查!
哇哇!没有检查!我不能改变我的php.ini!
真遗憾。通常共享主机不允许更改它们的php.ini文件,因此,遗憾的是,这个选项是不可用的。但是不要害怕!我们还有其他选择!
运行时配置
在所需的脚本中,我们可以在运行时更改php.ini条目!这意味着,它将在脚本运行时运行!甜蜜的!
error_reporting(E_ALL);
ini_set("display_errors", "On");
这两行代码与上面修改php.ini条目的效果相同!太棒了!
我仍然得到一个空白页/500错误!
这意味着脚本甚至还没有运行!这通常发生在语法错误的时候!
由于语法错误,脚本甚至无法进入运行时。它在编译时失败,这意味着它将使用php.ini中的值,如果没有更改,可能不允许显示错误。
错误日志
此外,PHP在默认情况下会记录错误。在共享主机中,它可能位于专用文件夹中,也可能位于与该脚本相同的文件夹中。
如果您可以访问php.ini,您可以在error_log条目下找到它。
如果错误是在PHP代码中,您可以在代码中使用error_reporting()函数来设置为报告所有。
但是,这并不能处理PHP崩溃的情况。这方面的信息只能在服务器日志中找到。也许你无法访问这些,但我合作过的许多托管提供商都有某种方式让你访问它。例如,我最喜欢的方法是在.php所在的当前目录中创建error_log文件。尝试在那里搜索或联系您的主机提供商。
我用这样的方法解决了我的500个问题:
A.检查php.ini参数
php.ini >> error_reporting = E_ALL | E_STRICT php.ini >> display_errors = On php.ini >> display_startup_errors =关闭
B.更新IIS管理器参数
IIS管理器>>错误页面>> 500 >>编辑功能设置>>详细错误
在这一步中,您将得到500个这样的错误,并且没有加载HTML。
IIS管理>> FastCGI设置>> php-cgi.exe >>标准错误模式>> IgnoreAndReurn200
在这一步中,您可以看到包含PHP错误的HTML页面,如下所示。
完成:)
您可以启用完整的错误报告(包括通知和严格消息)。有些人觉得这太啰嗦了,但值得一试。在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"
在页面顶部选择一个参数
error_reporting(E_ERROR | E_WARNING | E_PARSE);