-
<?php
-
-
$arr1 = array(1,3,5,7,9);
-
$arr2 = array(10,30,50,70,90);
-
-
foreach ($arr1 as $k => &$v) {
-
# do nothing
-
}
-
-
echo "================\n";
-
echo "$v\n";
-
-
foreach ($arr2 as $k => $v) {
-
# do nothing
-
}
-
-
echo "================\n";
-
echo "$v\n";
-
-
echo "================\n";
-
var_export($arr1);
-
echo "\n================\n";
-
var_export($arr2);
-
echo "\n================\n";
-
-
# 最后运行结果如下:
-
# 说明:foreach行中的变量的作用范围涵盖foreach行外的部分
-
/*
-
$ php test_bug.php
-
================
-
9
-
================
-
90
-
================
-
array (
-
0 => 1,
-
1 => 3,
-
2 => 5,
-
3 => 7,
-
4 => 90,
-
)
-
================
-
array (
-
0 => 10,
-
1 => 30,
-
2 => 50,
-
3 => 70,
-
4 => 90,
-
)
-
================
-
**/
-
-
?>
结论:foreach行中声明的变量的作用范围涵盖foreach行外的部分,只有{}大括号内声明的变量才是语句块变量范围
阅读(2014) | 评论(0) | 转发(0) |