简体   繁体   English

PHP 全球迭代迭代

[英]PHP Global Comportement On Iteration

I'm currently experimenting a weird comportment, while iterating on a global var another global var take it's value我目前正在尝试一个奇怪的行为,同时在一个全局变量上迭代另一个全局变量取它的值

This code这段代码

$lang_list = array('fr_FR', 'en_US');
$GLOBALS['lang_list'] = $lang_list;
if(isset($_GET['lang']) && in_array($_GET['lang'], $lang_list)){
    $GLOBALS['lang'] = $_GET['lang'];
}else{
    $GLOBALS['lang'] = 'en_US';
}

var_dump($GLOBALS['lang']);
foreach($GLOBALS['lang_list'] as $lang){
    var_dump($GLOBALS['lang']);
}

return string(5) "en_US" string(5) "fr_FR" string(5) "en_US"返回string(5) "en_US" string(5) "fr_FR" string(5) "en_US"

Is it expected due to something related to globals?是否由于与全局有关的某些事情而预期?

EDIT: Thanks everyone for the explanation: :)编辑:谢谢大家的解释::)

Yes, this is expected behavior.是的,这是预期的行为。 $GLOBALS['lang'] refers to $lang variable in the global scope. $GLOBALS['lang']引用全局 scope 中的$lang变量。 And there are only two scopes in PHP: global scope and local function scope (see this answer for more details).并且 PHP 中只有两个作用域:全局 scope 和本地 function Z31A1FD140BE4BEF2D11E12详细信息请参阅。

Since foreach loop doesn't create a local function scope, the $lang variable belongs to the global scope.由于foreach循环不会创建本地 function scope, $lang变量属于全局 scope。 Thus, $lang and $GLOBALS['lang'] both refer to the same variable.因此, $lang$GLOBALS['lang']都引用同一个变量。

$GLOBALS['lang'] refers to the same variable as $lang . $GLOBALS['lang']指的是与$lang相同的变量。 From the manual page for $GLOBALS :$GLOBALS的手册页

An associative array containing references to all variables which are currently defined in the global scope of the script.一个关联数组,包含对当前在脚本的全局 scope 中定义的所有变量的引用。 The variable names are the keys of the array.变量名是数组的键。

So when you change $lang in the global scope, it also changes $GLOBALS['lang'] (and vice versa, if you change $GLOBALS['lang'] it also changes $lang ).因此,当您在全局 scope 中更改$lang时,它也会更改$GLOBALS['lang'] (反之亦然,如果您更改$GLOBALS['lang']它也会更改$lang )。 Hence in your foreach loop, $GLOBALS['lang'] get assigned the values from $GLOBALS['lang_list'] in turn.因此,在您的foreach循环中, $GLOBALS['lang']依次从$GLOBALS['lang_list']中获得值。

Note that if you ran this code in a function, you would get your expected result, as $lang in this case would be defined in the function scope, not the global scope: Note that if you ran this code in a function, you would get your expected result, as $lang in this case would be defined in the function scope, not the global scope:

function test() {
    foreach($GLOBALS['lang_list'] as $lang){
        var_dump($GLOBALS['lang']);
    }
}

test();

Output: Output:

string(5) "en_US"
string(5) "en_US"

Demo on 3v4l.org 3v4l.org 上的演示

please read https://www.php.net/manual/en/reserved.variables.globals.php manual.请阅读https://www.php.net/manual/en/reserved.variables.globals.php手册。 You refer to the same value in the code above.您在上面的代码中引用了相同的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM