PhpStorm warning PHP variable might not have been defined

Viewed 12501

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?

2 Answers
Related