Using a variable from another condition in PHP

后端 未结 1 345
鱼传尺愫
鱼传尺愫 2021-01-29 09:06

I have this code:

if(!isset($_GET[\"act\"]))
{
    $display->display(\"templates/install_main.html\");
    if(isset($_POST[\"proceed\"]))
    {
        $prefi         


        
相关标签:
1条回答
  • 2021-01-29 09:40

    PHP's variable scope is function-level. $prefix would be available in your second if() IF the other if()'s evaluated to true and actually executed that $prefix = ... code.

    e.g.

    if (true) {
        $foo = 'bar'; // always executes
    }
    if (false) {
        $baz = 'qux'; // never executes
    }
    echo $foo; // works just fine
    echo $baz; // undefined variable, because $baz='qux' never executed.
    

    Also note that PHP is not capable of time travel:

    echo $x; // undefined variable;
    $x = 'y';
    echo $y; // spits out 'y'
    

    "earlier" code will not have "later" variables available, because the code that actually creates/assigns values to those variables won't have executed yet.

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