PHP的; foreach变量范围 [英] PHP for ; foreach variable scope

查看:89
本文介绍了PHP的; foreach变量范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

for/foreach循环中的变量是否具有局部作用域?如果是这样,我如何使其全球化?

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";                
?>

基本上,我在header.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>

如果要在该页面上,我希望列表具有 class ="mainNav active" ,否则,请为 class ="mainNav" .

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

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

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

所以我根据@urfusion建议创建了一个函数.现在page.php:

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";                
?>

还有header.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> 

什么也没做,现在我似乎已经丢失了echo语句的输出...

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

推荐答案

for/foreach循环中的变量是否具有局部作用域?如果是这样,我如何使其全球化?

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

PHP中只有两种作用域:全局作用域和局部函数作用域.

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

局部函数作用域包含函数的参数和在函数体内设置的变量.范围是在调用函数时创建的,并且在函数执行完成时(在执行return语句之后或在最后一条语句之后达到其结束}时)被销毁.

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

include d

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.

任何变量在其范围内均可用,因为它是第一次设置,直到使用 unset() 或直到其作用域被破坏.

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.

PHP文档.

回答您的问题:如果将forforeach循环放置在函数中,则它们定义的变量具有局部作用域(函数的作用域);否则它们具有全球范围.

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.

您的代码中的问题(缩进不对您有所帮助)出现在第一个foreach中.

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

这是正确缩进的代码:

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 />";
    }
}

问题很明显:它在每次迭代时都会修改变量$active的值.除最后一个$active分配外,其他所有指令均无用.只有最后一个才重要.而且,很可能在最后一次迭代中,它采用if ($title == $value)else分支,而$active变为''(空字符串).

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

有几个简单的解决方案.例如,您可以在上述foreach中显示菜单:

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
}

事实上,所有这些东西都应该放在header.php中.

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

这篇关于PHP的; foreach变量范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆