返回 PHP循环结构... 登陆

PHP循环结构

玄夜、 2019-07-04 11:12:18 243
<?php
//php循环结构

//php的循环共有四种:while、do while、for、foreach

//while循环  先判断后执行循环
$arr = [1,2,3,4,5,6,7,8,9,10];
$count = count($arr);

$i = 0;
$res = 0;
//while ($i<=$count-1){
//    $res = $res + $arr[$i];
//    $i ++;
//}
//echo $res;

//do while循环  先循环一次再执行判断
//do{
//    $res = $res + $arr[$i];
//    $i ++;
//}while($i<=$count-1);
//echo $res;


//for循环  根据循环条件来执行循环
//for ($i=0;$i<=$count-1;$i++){
//    $res = $res + $arr[$i];
//}
//echo $res;

//foreach循环  数组全部被遍历完之后才结束循环
foreach ($arr as $item) {
    $res = $res + $item;
}
echo $res;


最新手记推荐

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

全部回复(0)我要回复

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