我经常会尝试运行PHP脚本,然后得到一个空白屏幕。没有错误信息;只有一个空屏幕。原因可能是一个简单的语法错误(错误的括号,缺少分号),或者函数调用失败,或者完全是其他原因。
很难找出哪里出了问题。我最终注释掉了代码,到处输入“echo”语句,等等,试图缩小问题的范围。但肯定有更好的办法,对吧?
是否有一种方法可以让PHP像Java那样产生有用的错误消息?
我经常会尝试运行PHP脚本,然后得到一个空白屏幕。没有错误信息;只有一个空屏幕。原因可能是一个简单的语法错误(错误的括号,缺少分号),或者函数调用失败,或者完全是其他原因。
很难找出哪里出了问题。我最终注释掉了代码,到处输入“echo”语句,等等,试图缩小问题的范围。但肯定有更好的办法,对吧?
是否有一种方法可以让PHP像Java那样产生有用的错误消息?
当前回答
你需要从PHP中获取有用错误的两行关键代码是:
ini_set('display_errors',1);
error_reporting(E_ALL);
正如其他贡献者所指出的那样,出于安全原因,这些功能在默认情况下是关闭的。作为一个有用的技巧——当你设置你的网站时,为你的不同环境做一个切换是很方便的,这样在你的本地和开发环境中,这些错误默认是打开的。这可以通过以下代码实现(理想情况下是在index.php或配置文件中,这样从一开始就处于活动状态):
switch($_SERVER['SERVER_NAME'])
{
// local
case 'yourdomain.dev':
// dev
case 'dev.yourdomain.com':
ini_set('display_errors',1);
error_reporting(E_ALL);
break;
//live
case 'yourdomain.com':
//...
break;
}
其他回答
除了这里所有精彩的答案,我还想特别提一下MySQLi和PDO库。
为了……
总是看到数据库相关的错误,和 避免检查方法的返回类型以查看是否出现了错误
最好的选择是将库配置为抛出异常。
MySQLi
将此添加到脚本顶部附近
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
最好放在使用新的mysqli()或mysqli_connect()之前。
PDO
在连接实例上将PDO::ATTR_ERRMODE属性设置为PDO::ERRMODE_EXCEPTION。你可以在构造函数中这样做
$pdo = new PDO('driver:host=localhost;...', 'username', 'password', [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);
或者在创造之后
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
我推荐Nette Tracy来更好地可视化PHP中的错误和异常:
有一个非常有用的扩展名为“xdebug”,它也会使您的报告更好。
对于那些使用nginx并且即使文件<?PHP echo 123;在我的情况下,我在nginx配置文件中没有这个必要的PHP选项:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_params文件中没有这个选项,所以PHP不起作用,日志中也没有任何错误。
这个答案是由冗余部门为您带来的。
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.