abstract:<?phpheader('content-type:text/html;charset=utf-8');//创建一个foreach循环$arr=[11,32,12,63,67,98];$y=0;foreach ($arr as $key => $value) {$x = $value;$y += $x;echo $value.'----'.$y.'
<?php
header('content-type:text/html;charset=utf-8');
//创建一个foreach循环
$arr=[11,32,12,63,67,98];
$y=0;
foreach ($arr as $key => $value) {
$x = $value;
$y += $x;
echo $value.'----'.$y.'<br>';
}
echo '<hr>';
echo 'y='.$y.'<br>';
echo '循环变量$y可被访问'.'<hr>'.'<br>';
$prac = function(){
$array=[11,32,12,63,67,98];
$b=0;
foreach ($array as $k => $v) {
$a = $v;
$b += $a;
echo $v.'----'.$b.'<br>';
}
echo '$b='.$b;
};
$prac();
echo '<hr>';
echo isset($b)? '函数外能访问作用域内的循环变量$b' : '函数外不能访问作用域内的循环变量$b'.'<hr>';