PHP for ; foreach variable scope

前端 未结 2 1895
傲寒
傲寒 2020-12-20 02:21

Do variables in the for / foreach loop have a local scope? If so, how do I make it global?

page.php:



        
相关标签:
2条回答
  • 2020-12-20 03:14

    Do variables in the for / foreach loop have a local scope? If so, how do I make it global?

    There are only two kinds of scopes in PHP: the global scope and local function scope.

    A local function scope contains the function's parameters and the variables that are set inside the function body. The scope is created when the function is invoked and it is destroyed when the execution of the function completes (either after it executes a return statement or when it reaches its closing } after the last statement).

    The global scope contains all the variables that are set by the code outside any function. It is created by the main script (the one invoked by the interpreter).

    The included and required files do not create new scopes. The code that is outside functions in included files runs in the scope where the include or require statement is placed. This means, global scope if the include statement appears outside any function of the local scope of the function that contains the include statement. All four include statements (include, include_once, require, require_once) work the same regarding this matter.

    Any variable is available in its scope since it was set for the very first time until it is removed using unset() or until its scope is destroyed.

    Read more about variables scope on the PHP documentation.


    To answer your question: if the for or foreach loop is placed in a function then the variables they define have local scope (the scope of the function); otherwise they have global scope.


    The problem in your code (the bad indentation doesn't help you see it) is in the first foreach.

    This is the code properly indented:

    foreach ($menu as $value) {
        if ($title == $value) {
            $active = "active";
            echo "if " . $active. $title . $menu[$x] ." <br /><br />";
        } else {
            $active = "";
            echo "else " . $active. $title . $menu[$x] ." <br /><br />";
        }
    }
    

    The problem is obvious: it modifies the value of variable $active on each iteration. All but the last assignment of $active is useless. Only the last one counts. And, most probably, on the last iteration it takes the else branch of if ($title == $value) and $active becomes '' (the empty string).

    There are several simple solutions for the problem. For example, you can display the menu inside the aforementioned foreach:

    foreach ($menu as $value) {
        if ($title == $value) {
            $active = "active";
        } else {
            $active = "";
        }
        ?>
        <li class="mainNav <?php echo $active; ?>" style="z-index:8">
        <a href="http://www.com"><?php echo $value; ?></a></li>
        <?php
    }
    

    In fact, all this stuff should go into header.php.

    0 讨论(0)
  • 2020-12-20 03:17

    your code should look like

    $active = "";
    foreach ($menu as $value){ 
    
       if ($title == $value){   
           $active = "active";
           echo "if " . $active. $title . $menu[$x] ." <br /><br />";
       } 
       else {
         $active = "";
         echo "else " . $active. $title . $menu[$x] ." <br /><br />";
       }
    }
    

    In PHP, variables can be declared anywhere in the script.

    The scope of a variable is the part of the script where the variable can be referenced/used.

    PHP has three different variable scopes:

    local
    global
    static
    

    for more information on scope

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