摘要:<?php //for循環(huán)是計(jì)數(shù)型循環(huán) function sum($mix,$max){ &n
<?php
//for循環(huán)是計(jì)數(shù)型循環(huán)
function sum($mix,$max){
$res=range($mix,$max);
echo '<pre>',var_export($res,true),'<br>';
$count=count($res);
echo $count,'<br>';
$total=0;
$i=0;
for($i=0;$i<$count;$i++){
$total+=$res[$i];
}
echo $total;
}
sum(5,10);
//while()入口判斷型循環(huán),用于一維數(shù)組循環(huán)
function sum($mix,$max){
$res=range($mix,$max);
echo '<pre>',var_export($res,true),'<br>';
$count=count($res);
echo $count,'<br>';
$total=0;
$i=0;
while(isset($res[$i])){
$total+=$res[$i];
$i++;
}
echo $total;
}
sum(0,10);
//foreach()入口判斷型循環(huán),多用于二維數(shù)組循環(huán)
$res=[
['id'=>1,'name'=>'admin','qq'=>'13897290016'],
['id'=>2,'name'=>'root', 'qq'=>'13897290017'],
['id'=>3,'name'=>'user', 'qq'=>'13897290018'],
['id'=>4,'name'=>'super','qq'=>'13897290019']
];
//聲名一個(gè)空數(shù)組,獲取鍵名值
$skey=[];
//用內(nèi)部指針輸出鍵名及鍵名對(duì)應(yīng)的值,就不必指定$var['鍵名'];
foreach($res as $key=>$var){
// echo count($var);
echo key($var).':'.current($var).'-----';
next($var);
echo key($var).':'.current($var).'-----';
next($var);
echo key($var).':'.current($var),'<br>';
//把鍵名返回到$skey數(shù)組中
$skey=array_keys($var);
}
echo var_export($skey,true);
批改老師:天蓬老師批改時(shí)間:2019-08-16 09:50:32
老師總結(jié):實(shí)際開發(fā)中, 基本上都是可以用foreah來(lái)完成的