Loops
while loop<?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?> 會先檢查看看 $x 有沒有小於等於 5.
do...while loop
<?php
$x = 6;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?> 會先執行一次,然後才去檢查 $x 有沒有小於等於 5.
for loop
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
foreach loop
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>
key 和 value
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
foreach($age as $x => $val) {
echo "$x = $val<br>";
}
?>
break
<?php
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
break;
}
echo "The number is: $x <br>";
}
?> 0 1 2 3
continue
<?php
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
continue;
}
echo "The number is: $x <br>";
}
?>0 1 2 3 5 6 7 8 9
頁:
[1]