<?php //PHP中循环的三种方式 //1、for 通常知道循环次数的可以使用 //2、while //3、foreach 主要用于数组的遍历 $min = 1; $max = 10; $data = range($min,$max); $res = 0; $count = count($data); //print_r($data); //1.for循环 for($i=0;$i<$count;$i++) { $res += $data[$i]; } echo $res . '<hr>'; //2.while 循环 $i = 0;//初始化循环次数 $res = 0; while($i<$count) { $res = $res + $data[$i]; $i++; } echo $res . '<hr>'; //3.foreach foreach($data as $key => $val){ echo $key .'=>' .$val .'<br>'; }