redd 發表於 2020-10-20 17:57:09

Loops

while loop
<?php
$x = 1;

while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?> 會先檢查看看 $x 有沒有小於等於 5.



redd 發表於 2020-10-20 18:01:09

do...while loop
<?php
$x = 6;

do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?> 會先執行一次,然後才去檢查 $x 有沒有小於等於 5.



redd 發表於 2020-10-20 18:04:39

for loop
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>



redd 發表於 2020-10-20 18:07:17

foreach loop
<?php
$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $value) {
echo "$value <br>";
}
?>



redd 發表於 2020-10-20 18:09:48

key 和 value
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

foreach($age as $x => $val) {
echo "$x = $val<br>";
}
?>



redd 發表於 2020-10-20 19:29:11

break
<?php
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
    break;
}
echo "The number is: $x <br>";
}
?> 0 1 2 3



redd 發表於 2020-10-20 19:30:43

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]
查看完整版本: Loops