Perhaps phpstorm warning php variable was not defined

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

PHP above the header is set to $ smith. In the body, I get a warning from $ smith saying that it can be undefined. If I declare the whole variable at the top of the PHP code, ($ smith = "";), it is happy (without warning).

Is there something I should do to prevent these warnings?

I don’t like the idea of ​​adding a comment to everyone who says so as not to check it, and I don’t want to turn them off.

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

Any thoughts?

+5
2

PHPStorm undefined variables, require on include statement . 'Undefined variable' - Ignore 'include' 'require'. , .

enter image description here

. File > Settings (Ctrl+Alt+S) > Project Settings > Inspections > PHP > Undefined > Undefined variable

+14

, , , . :

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

, :

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

, , , $smith ( $submit "button_a" "button_b")), , $smith, , ​​ script.

+6

All Articles