我经常会尝试运行PHP脚本,然后得到一个空白屏幕。没有错误信息;只有一个空屏幕。原因可能是一个简单的语法错误(错误的括号,缺少分号),或者函数调用失败,或者完全是其他原因。
很难找出哪里出了问题。我最终注释掉了代码,到处输入“echo”语句,等等,试图缩小问题的范围。但肯定有更好的办法,对吧?
是否有一种方法可以让PHP像Java那样产生有用的错误消息?
我经常会尝试运行PHP脚本,然后得到一个空白屏幕。没有错误信息;只有一个空屏幕。原因可能是一个简单的语法错误(错误的括号,缺少分号),或者函数调用失败,或者完全是其他原因。
很难找出哪里出了问题。我最终注释掉了代码,到处输入“echo”语句,等等,试图缩小问题的范围。但肯定有更好的办法,对吧?
是否有一种方法可以让PHP像Java那样产生有用的错误消息?
当前回答
你也可以像这样在终端(命令行)运行文件:
这将运行您的代码,并为您提供与在error.log中看到的任何错误相同的输出。它提到了错误和行号。
其他回答
下面将启用所有错误:
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
请参见以下链接
http://php.net/manual/en/errorfunc.configuration.php#ini.display-errors http://php.net/manual/en/errorfunc.configuration.php#ini.display-startup-errors http://php.net/manual/en/function.error-reporting.php
对于语法错误,您需要在php.ini中启用错误显示。默认情况下,这些是关闭的,因为您不希望“客户”看到错误消息。查看PHP文档中关于error_reporting和display_errors这两个指令的信息。Display_errors可能是您想要更改的。如果你不能修改php.ini,你也可以在。htaccess文件中添加以下代码:
php_flag display_errors on
php_value error_reporting 2039
您可以考虑在您的PHP版本中使用E_ALL的值(如Gumbo所提到的)用于error_reporting以获取所有错误。更多信息
(1)您可以检查错误日志文件,因为它将有所有的错误(除非日志记录已被禁用)。(2)添加以下两行代码将帮助您调试非语法错误:
error_reporting(-1);
ini_set('display_errors', 'On');
另一种选择是使用编辑器,当你打字时检查错误,如PhpEd。PhpEd还提供了一个调试器,可以提供更详细的信息。(PhpEd调试器与xdebug非常相似,并直接集成到编辑器中,因此您可以使用一个程序来完成所有工作。)
卡特曼的链接也很好:http://www.ibm.com/developerworks/library/os-debug/
使用@inexistent_function_call ();将导致解释器安静地停止并中止脚本解析。您应该检查无效的函数,并尽量不要使用错误抑制操作符(@ char)
这个答案是由冗余部门为您带来的。
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中注册自己的错误处理程序。例如,在这些模糊的情况下,将所有错误转储到一个文件可能会对您有所帮助。注意,无论当前error_reporting设置为什么,函数都会被调用。非常基本的例子:
function dump_error_to_file($errno, $errstr) {
file_put_contents('/tmp/php-errors', date('Y-m-d H:i:s - ') . $errstr, FILE_APPEND);
}
set_error_handler('dump_error_to_file');