phpstorm警告php变量可能尚未定义 [英] phpstorm warning php variable might not have been defined

查看:948
本文介绍了phpstorm警告php变量可能尚未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?php
//  $smith = "";
  $submit ="button_a";

  if($submit == "button_a") {
      $smith = "button_a";
  }
    elseif($submit == "button_b"){
      $smith = "button_b";
  }
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>

<p>
    <?php echo($smith);  ?>
</p>

</body>
</html>

PHPSTORM为每个文件提供一个标志:红色-错误,黄色-警告,绿色-确定.

PHPSTORM provides a flag for each file: Red - Errors, Yellow - Warning, Green - OK.

标题上方的PHP将为$ smith分配一个值.在正文中,我收到$ smith的警告,说它可能是未定义的.如果我在PHP代码的顶部声明所有变量,则($ smith =";)会很高兴(没有警告).

The PHP above the header will assign a value to $smith. In the body, I get a warning on $smith saying that it might be undefined. If I declare all of the variable at the top of the PHP code, ($smith = "";) it is happy (no warning).

我应该做些什么来防止这些警告?

Is there something that I should be doing to prevent these warnings?

我不喜欢将评论附加到每个人的发言中以不进行检查,并且我不想将其全部关闭.

I don't like the idea of attaching the comment to each one saying to not check it and I don't want to turn them all off.

当我包含定义四个或五个变量的db_login.php文件时,这种情况经常发生.我有用于WAMP,MAMP和实际软管的不同db_login.php文件.

This happens a lot when I include my db_login.php file which defines four or five variables. I have different db_login.php files for WAMP, MAMP, and the real hose.

有什么想法吗?

推荐答案

如果在变量访问之前,对include语句的需求位于同一执行流中,则可以告诉PHPStorm忽略未定义的变量报告.您会在未定义变量"中找到它-忽略"include"和"require"语句.默认情况下已启用它,因此您应该禁用它.

You can tell PHPStorm to ignore undefined variables reports if require on include statement are located in the same execution flow before the variable access. You'll find it in 'Undefined variable' - Ignore 'include' and 'require' statements. It is enabled by default, so you should disable it.

注意:设置位于File > Settings (Ctrl+Alt+S) > Project Settings > Inspections > PHP > Undefined > Undefined variable

这篇关于phpstorm警告php变量可能尚未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆