6

Consider:

<?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 provides a flag for each file: Red - Errors, Yellow - Warning, Green - OK.

The PHP code 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.

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.

Any thoughts?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mkstlwtz
  • 720
  • 2
  • 7
  • 19
  • 2
    I would thoroughly recommend tho always declaring/initialising a variable before any use... makes debugging so much easier. – Brian Aug 01 '12 at 08:52
  • ... and not to use global variables for settings (e.g. db details etc -- static class for a help) -- if it's your own code, of course :) – LazyOne Aug 01 '12 at 13:17

2 Answers2

17

You can tell PhpStorm to ignore undefined variables reports if require on include statements 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.

Enter image description here

*Note: The setting is in menu FileSettings (Ctrl + Alt + S) → Project SettingsInspectionsPHPUndefinedUndefined variable

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dreen
  • 6,976
  • 11
  • 47
  • 69
7

Yeah, there are two things you can do to get rid of this warning. What you said:

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

Or check if it's set when you print it:

<?php
    if( isset( $smith)) {
        echo($smith);
    }
?>

However, this is just a warning, and it is letting you know that there is a condition that $smith won't be defined (when $submit isn't "button_a" and isn't "button_b"). If that condition were to occur, you would be printing $smith when it wasn't set, which could be a bug in your script.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nickb
  • 59,313
  • 13
  • 108
  • 143
  • 1
    PHPSTORM is smarter than I had thought. If I change elseif to else so that all paths through the code assign a value, it is happy. What about my db_login include file that defines database, user, password, table, etc. Any way to make it "look into" the include file so that I don't have to predefine them all? – mkstlwtz Aug 01 '12 at 01:30
  • Because then there's no conditions that exist in which `$smith` wouldn't be defined. It's actually helpful (IMO), as it's telling you that a condition exists where you'd be accessing a variable that hasn't been defined. You'd only get this info from PHP if you executed the script with the "bug" conditions (i.e. where `$submit` isn't `button_a` or `button_b`). For your include file issues, see Dreen's answer below. – nickb Aug 01 '12 at 01:32