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?

2
  • 2
    I would thoroughly recommend tho always declaring/initialising a variable before any use... makes debugging so much easier. Commented Aug 1, 2012 at 8: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 :) Commented Aug 1, 2012 at 13:17

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

Where are these settings?
File > Settings (Ctrl+Alt+S) > Project Settings > Inspections > PHP > Undefined > Undefined variable
Thanks Kobra. @Dreen: pls update the answer, as screenshot really lacks context.
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.

2 Comments

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?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.