循環語句
1)for循環:
重復執行一個代碼塊指定的次數。
for ($i = 0; $i < 5; $i++) { // 初始化 $i 為 0,每次循環后將 $i 值增加 1,當 $i 小于 5 時執行循環echo "The number is: $i \n"; // 輸出當前 $i 的值并換行
}// 循環輸出結果為:
// The number is: 0
// The number is: 1
// The number is: 2
// The number is: 3
// The number is: 4
2)while循環:
在指定條件為真時重復執行代碼塊。
$x = 1; // 定義變量$x并賦值為1
while ($x <= 5) { // 當$x小于等于5時執行循環echo "The number is: $x \n"; // 輸出當前$x的值并換行$x++; // 將$x的值加1
}// 循環輸出結果為:
// The number is: 1
// The number is: 2
// The number is: 3
// The number is: 4
// The number is: 5