PHP for ; foreach variable scope

13,103

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.

Share:
13,103
testing123
Author by

testing123

Updated on June 04, 2022

Comments

  • testing123
    testing123 almost 2 years

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

    page.php:

    <?php
    $title = "2";                  
    $menu[0] = "1";   
    $menu[1] = "2";
    $menu[2] = "3";
    $menu[3] = "4";
    $menu[4] = "5";
    $menu[5] = "6";
    $menu[6] = "7";
    $menu[7] = "8";
    
    
    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 />";
    }}
    
    include "header.php"; 
    
    foreach ($menu as $value) {
    var_dump($active);
        echo "$value <br>";
    }
    
    
    include "header.php"; 
    ?>
    
    <!-- begin page content -->
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    <!-- end page content -->
    
    <?php
    include "footer.php";                
    ?>
    

    Essentially, I have this line in the header.php:

    <li class="mainNav <?php echo $active; ?>" style="z-index:8">
    <a href="http://www.com"><?php echo $menu[0]; ?></a></li>
    

    I want the list to have class="mainNav active" if it's that page and class="mainNav" if not.

    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    So I created a function from @urfusion suggestion. Now page.php:

    <?php
    $title = "2";                  
    $menu[0] = "1";   
    $menu[1] = "2";
    $menu[2] = "3";
    $menu[3] = "4";
    $menu[4] = "5";
    $menu[5] = "6";
    $menu[6] = "7";
    $menu[7] = "8;
    ?>
    
    
    <?php
    
    
    function mainNav($menu) {
    
    
    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 />";
        }  
    
    
    echo "function" . $active . $value;
      return $active;
    
    }
    }
    
    
    include "header.php"; 
    ?>
    
    
    <!-- begin page content -->
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    <!-- end page content -->
    
    <?php
    include "footer.php";                
    ?>
    

    And the header.php:

     <li class="mainNav <?php mainNav(); ?>" style="z-index:8">
    <a href="http://www.com"><?php echo $menu[1]; ?></a></li> 
    

    Still nothing and now I seem to have lost the output of the echo statements ...

  • testing123
    testing123 over 8 years
    I'm getting the correct echo statements but it's not changing the header.php. I'm wondering if that's because the foreach loop is not contained in the header.php document or if it's something else ...
  • urfusion
    urfusion over 8 years
    you can not use a variable in other file without passing it through any function or url or session variable.
  • VolkerK
    VolkerK over 8 years
    Only the last value that has been assigned to $active will be retained when header.php is included (after the loop). In your example the last assignement is in the loop iteration having $value==8 and therfore $active=''.
  • VolkerK
    VolkerK over 8 years
    "you can not use a variable in other file without passing it through any function or url or session variable" - what? docs.php.net/manual/en/language.variables.scope.php says otherwise: "For the most part all PHP variables only have a single scope. This single scope spans included and required files as well."