PHP 为 ;foreach 变量作用域 [英] PHP for ; foreach variable scope
问题描述
for/foreach 循环中的变量有局部作用域吗?如果是这样,我如何使它成为全球性的?
page.php:
<!-- 开始页面内容-->!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!<!-- 结束页面内容--><?php包括footer.php";?>
本质上,我在 header.php 中有这一行:
<a href="http://www.com"><?php echo $menu[0];?></a></li>
如果是该页面,我希望列表具有 class="mainNav active",如果不是,则具有 class="mainNav".
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
所以我根据@urfusion 建议创建了一个函数.现在 page.php:
<?php函数 mainNav($menu) {foreach ($menu 作为 $value){如果($title == $value){$active = "活动";回声如果".$活跃.$title .$menu[$x] ." <br/><br/>";}别的 {$active = " ";回声其他".$活跃.$title .$menu[$x] ." <br/><br/>";}回声功能".$活跃.$值;返回 $active;}}包括header.php";?><!-- 开始页面内容-->!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!<!-- 结束页面内容--><?php包括footer.php";?>
和 header.php:
<a href="http://www.com"><?php echo $menu[1];?></a></li>
仍然什么都没有,现在我似乎丢失了 echo 语句的输出......
for/foreach 循环中的变量有局部作用域吗?如果是这样,我如何使它成为全球性的?
PHP 中只有两种作用域:全局作用域和局部函数作用域.
局部函数作用域包含函数的参数和在函数体内设置的变量.作用域在函数被调用时被创建,在函数执行完成时被销毁(在它执行一个 return
语句之后或者当它到达它的结束 }
之后最后一句).
全局作用域包含由任何函数之外的代码设置的所有变量.它由主脚本(由解释器调用的脚本)创建.
include
d 和 require
d 文件不会创建新的作用域.包含文件中函数外部的代码在放置 include
或 require
语句的范围内运行.这意味着,如果 include
语句出现在包含 include
语句的函数的局部范围的任何函数之外,则为全局范围.所有四个包含语句(include
、include_once
、require
、require_once
)在这个问题上的作用相同.>
任何变量在其作用域内都是可用的,因为它是第一次设置,直到使用 unset()
或直到其作用域被销毁.
回答你的问题:如果 for
或 foreach
循环被放置在一个函数中,那么它们定义的变量具有局部作用域(函数的作用域);否则它们具有全局作用域.
您的代码中的问题(错误的缩进不能帮助您看到它)出在第一个 foreach
.
这是正确缩进的代码:
foreach ($menu as $value) {如果($title == $value){$active = "活动";回声如果".$活跃.$title .$menu[$x] ." <br/><br/>";} 别的 {$active = "";回声其他".$活跃.$title .$menu[$x] ." <br/><br/>";}}
问题很明显:它会在每次迭代时修改变量 $active
的值.除了 $active
的最后一个赋值之外,所有的东西都是无用的.只有最后一项重要.而且,最有可能的是,在最后一次迭代中,它采用了 if ($title == $value)
的 else
分支,并且 $active
变成了 ''
(空字符串).
这个问题有几个简单的解决方案.比如你可以在前面提到的foreach
里面显示菜单:
foreach ($menu as $value) {如果($title == $value){$active = "活动";} 别的 {$active = "";}?><li class="mainNav <?php echo $active; ?>"style="z-index:8"><a href="http://www.com"><?php echo $value;?></a></li><?php}
事实上,所有这些东西都应该放在 header.php
中.
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 ...
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 include
d and require
d 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
.
这篇关于PHP 为 ;foreach 变量作用域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!