php循环语句遍历数组

Original 2019-02-25 20:51:16 259
abstract:<?phpfunction add($min,$max){ $arr=range($min,$max); $total=0; for ($i=$min;$i<$max+1;$i++){ $total+=$i; } return $total; }echo add(1,100);function add1($min,$max){ $arr=range($min,$max); $coun

<?php

function add($min,$max){

$arr=range($min,$max);

$total=0;

for ($i=$min;$i<$max+1;$i++){

$total+=$i;

}

return $total;

}

echo add(1,100);


function add1($min,$max){

$arr=range($min,$max);

$count=count($arr);

$total=0;

$i=0;

while ($i<$count){

$total+=$arr[$i];

$i++;

}

return $total;

}

echo add1(1,100);


function add2($min,$max){

$arr=range($min,$max);

$count=count($arr);

$total=0;

$i=0;

do{

$total+=$arr[$i];

$i++;

}while ($i<$count);

return $total;

}

echo add2(1,100);


function add3($min,$max){

$arr=range($min,$max);

$count=count($arr);

$total=0;

foreach ($arr as $value) {

$total+=$value;

}

return $total;

}

echo add3(1,100);

for,while.do while,foreach循环

Correcting teacher:查无此人Correction time:2019-02-26 09:10:04
Teacher's summary:写的不错。下次把代码缩进,看着整齐。继续加油

Release Notes

Popular Entries