返回php循环语句......登陆

php循环语句遍历数组

兰岚2019-02-25 20:51:16123

<?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循环

最新手记推荐

• 用composer安装thinkphp框架的步骤• 省市区接口说明• 用thinkphp,后台新增栏目• 管理员添加编辑删除• 管理员添加编辑删除

全部回复(0)我要回复

暂无评论~
  • 取消回复发送
  • PHP中文网