Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
684 views
in Technique[技术] by (71.8m points)

loops - PHP for ; foreach variable scope

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...