PHP for ; foreach variable scope

2019-06-10 13:06发布

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

2条回答
唯我独甜
2楼-- · 2019-06-10 13:44

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

查看更多
叼着烟拽天下
3楼-- · 2019-06-10 13:59

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.

查看更多
登录 后发表回答