while
例子:
/* example 1 */ $a = 0; while (true) { $a++; echo $a.'<br>'; if($a >= 10){ break; //打断循环 } } /* example 2 */ $a = 0; while ($a < 10) { $a++; echo $a.'<br>'; } /* example 3 */ $arr = array("orange", "banana", "apple", "raspberry"); $i = 0; while ($i < count($arr)) { $a = $arr[$i]; echo $a .","; $i++; } /* example 4 */ $myArray=['aa','bb','cc','dd']; while (list ($key, $val) = each ($myArray)) { echo "$key=>$val,"; }
do-while
例子:
$a=0; do{ $a++; echo $a; } while ($a < 10);
for
例子:
/* example 1 */ for ($i = 1; $i <= 10; $i++) { echo $i; } echo "<br>"; /* example 2 */ for ($i = 1; ; $i++) { if ($i > 10) { break; } echo $i; } echo "<br>"; /* example 3 */ $i = 1; for (;;) { if ($i > 10) { break; } echo $i; $i++; } echo "<br>"; /* example 4 */ for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);
foreach
例子:
$arr = array(1, 2, 3, 4); /* example 1 */ foreach ($arr as $value) { echo $value.'<br>'; } /* example 2 */ foreach ($arr as $key => $val) { echo "$key=>$val,"; }