6

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

4
  • 1
    initialize $active before foreach loop Commented Jan 6, 2016 at 16:11
  • "Do variables in the for / foreach loop have a local scope?" - loops, switch/cases, try-catch blocks etc do not constitute scopes in php. see 3v4l.org/WjX5Q Commented Jan 6, 2016 at 16:17
  • I am not sure what you are trying to do. the second foreach completely throw me off! Commented Jan 6, 2016 at 16:19
  • Are you even sure the $title is a global, as in, usable below in your method? To me it looks like you should be using a clause like using (...) in your function declaration Commented Jan 6, 2016 at 19:49

2 Answers 2

18

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.

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

The included and required files do not create new scopes. The code that is outside functions in the 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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

4 Comments

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 ...
you can not use a variable in other file without passing it through any function or url or session variable.
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=''.
"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."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.