错误还是黑客? $全局变量
$GLOBALS["items"] = array('one', 'two', 'three', 'four', 'five' ,'six', 'seven');
$alter = &$GLOBALS["items"]; // Comment this line
foreach($GLOBALS["items"] as $item) {
echo get_item_id();
}
function get_item_id(){
var_dump(key($GLOBALS["items"]));
}
检查此代码的输出,包括注释和未注释的第二行。 我的结果(PHP 5.3.0)。 有第二行
int(1) int(2) int(3) int(4) int(5) int(6) NULL
没有第二行:
int(1) int(1) int(1) int(1) int(1) int(1) int(1)
为什么结果这么奇怪?
$GLOBALS["items"] = array('one', 'two', 'three', 'four', 'five' ,'six', 'seven');
$alter = &$GLOBALS["items"]; // Comment this line
foreach($GLOBALS["items"] as $item) {
echo get_item_id();
}
function get_item_id(){
var_dump(key($GLOBALS["items"]));
}
Check output of this code, with commented and uncommented second line.
My result(PHP 5.3.0).
With second line
int(1) int(2) int(3) int(4) int(5) int(6) NULL
Without second line:
int(1) int(1) int(1) int(1) int(1) int(1) int(1)
Why so strange result?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个可能的解释:
我们知道
foreach
总是循环如果未引用数组的副本,则覆盖该数组的副本:这意味着原始数组的内部指针不会更改,并且
key()
将始终返回相同的值(正如我们注释掉该行时所看到的那样)。事实上,如果我们执行var_dump($GLOBALS)
,我们会得到:(无引用)
但是一旦我们生成对数组的引用(使用
$alter
),$GLOBALS['items']
也成为引用,因为两个条目必须指向同一个数组:因此,
foreach
循环确实会迭代原始数组array 并更改内部指针,这会影响key()
。总结一下:这是引用的问题,而不是
$GLOBALS
的问题。Here is a possible explanation:
We know that
foreach
always loops over a copy of the array if it is not referenced:That means that the internal pointer of the original array is not changed and
key()
will always return the same value (as we can see when we comment out the line). And indeed if we do avar_dump($GLOBALS)
, we get:(no reference)
But as soon as we generate a reference to the array (with
$alter
),$GLOBALS['items']
becomes a reference too, because both entries have to point to the same array:Hence, the
foreach
loop does iterate over the original array and changes the internal pointer, which affectskey()
.To sum up: It is a problem with references, not with
$GLOBALS
.