<?php
//----------------for(循环变量初始化;循环条件;循环变量的更新){} for循环----------------
function add($min,$max){
$data = range($min,$max);
$count = count($data);
$res = 0;
for ($i = 0;$i<$count;$i++){
$res +=$data[$i];
}
return $res;
}
echo add(1,10).'<hr >';
//----------------while----------------
//循环变量初始化在while前面,接着whlie(循环的条件){循环变量的更新}
function add01($min,$max){
$data = range($min,$max);
$count = count($data);
$res = 0;
$i = 0;
while($i<$count){
$res +=$data[$i];
$i++;
}
return $res;
}
echo add01(1,5).'<hr >';
//----------------do{}while(循环条件);把循环内向先循环一次再做判断。至少会执行一次----------------
function add02($min,$max){
$data = range($min,$max);
$count = count($data);
$res = 0;
$i = 0;
do{
$res +=$data[$i];
$i++;
}while($i<$count);
return $res;
}
echo add02(1,6).'<hr >';
//----------------foreach----------------
//foreach (数组 as $key=>$value)
//foreach (数组 as $value)
$data = range(0,5);
foreach ($data as $key=>$value){
echo $key.'---'.$value.'<br />';
};
foreach ($data as $value){
echo $value.'<br />';
};