如何在for循环外获取for循环里的值

如题所述

int temp,index;
temp=0;
for(index=0;index<10;index++)
temp+=index;
后面直接调用temp的值就可以了。
温馨提示:内容为网友见解,仅供参考
第1个回答  2011-04-15
把循环里值作为参数传递到调用的函数里;
或者用全局静态变量做循环里的变量值
在循环里修改某个全局变量的值为需要的值
第2个回答  推荐于2017-09-01
第一种:
$nextdate = ‘’;
for($i=0;$i<12;$i++){
$nextdate .= date("Y-m",strtotime("- $i month")).'<br />';
}
echo $nextdate;

第二种,用数组。
如:
$nextdate = array();
for($i=0;$i<12;$i++){
$nextdate[] = date("Y-m",strtotime("- $i month"));
}

然后用 foreach 遍历 $nextdate
相似回答