phpstorm warning php variable might not have been defined

前端 未结 2 1914
天涯浪人
天涯浪人 2020-12-09 13:59


        
相关标签:
2条回答
  • 2020-12-09 14:15

    Yeah, there's 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.

    0 讨论(0)
  • 2020-12-09 14:20

    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.

    enter image description here

    Note: The setting is in File > Settings (Ctrl+Alt+S) > Project Settings > Inspections > PHP > Undefined > Undefined variable

    0 讨论(0)
提交回复
热议问题