我经常会尝试运行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.
其他回答
在页面顶部选择一个参数
error_reporting(E_ERROR | E_WARNING | E_PARSE);
错误和警告通常出现在....\logs\php_error.log或....\logs\apache_error.log中,具体取决于您的php.ini设置。
此外,有用的错误通常指向浏览器,但由于它们不是有效的html,因此不会显示。
所以"tail -f"你的日志文件,当你得到一个空白屏幕使用IEs "view" -> "source"菜单选项来查看原始输出。
使用@inexistent_function_call ();将导致解释器安静地停止并中止脚本解析。您应该检查无效的函数,并尽量不要使用错误抑制操作符(@ char)
对于快速,动手故障排除,我通常建议在这里SO:
error_reporting(~0); ini_set('display_errors', 1);
放在正在进行故障排除的脚本的开头。这并不完美,完美的变体是你也在PHP .ini中启用它,并在PHP中记录错误以捕捉语法和启动错误。
这里列出的设置显示所有错误、通知和警告,包括严格的错误,而不考虑使用哪个PHP版本。
接下来要考虑的事情:
安装Xdebug并在IDE中启用远程调试。
也可以看到:
错误报告(PHP正确方式) 预定义ConstantsDocs error_reporting()文档 display_errorsDocs
打开错误报告是正确的解决方案,但是它似乎不会在打开它的程序中生效,而只会在随后包含的程序中生效。
因此,我总是创建一个文件/程序(我通常称之为“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¶m=val').done(function(data) { /* ... */