PHP While 循环
核对 PHP 四类循环语句定位,以及 while(先判断后执行)与 do...while(先执行后判断)在初始条件真假、递增时机与 break/continue 控制下的实测差异。
学完这一章,你能够
- 核对 PHP 四类循环语句定位,以及 while(先判断后执行)与 do...while(先执行后判断)在初始条件真假、递增时机与 break/continue 控制下的实测差异。
PHP 四类循环结构概览
循环语句用于按条件或计数重复执行同一段代码块。PHP 提供四种循环:`while` 在条件成立时持续重复;`do...while` 至少执行一次后再检查条件;`for` 适合已知明确迭代次数的计数循环;`foreach` 专用于遍历数组与可迭代对象(`for` 与 `foreach` 将在下一章展开)。
while 循环:先判条件后执行
`while (条件) { ... }` 每次迭代前先计算条件,为真才进入循环体,否则直接结束。将 `$i` 初始化为 `1`,循环条件 `$i <= 5` 且每轮先输出再 `$i++`,本站 PHP 8.4.27 实测依次产生 `The number is 1` 至 `The number is 5`,退出时 `$i === 6`。使用 `while` 时务必确保循环体内存在推动条件最终为假的更新语句,并可结合 `continue` 跳过单轮或 `break` 提前终止(实测过滤 `1..9` 奇数并在 `7` 处 `break` 得 `[1, 3, 5]`)。
do...while 循环:至少执行一次
`do { ... } while (条件);`(注意末尾分号)先执行一次循环体再判断条件。当 `$j = 1` 且循环体先执行 `$j++` 再输出时,实测输出 `The number is 2` 至 `The number is 6`(共 5 轮,因为输出 `6` 后 `$j <= 5` 才变为假)。更能体现二者本质区别的边界实验:当初始值为 `10` 而条件为 `<= 5` 时,`while` 执行 `0` 次,`do...while` 仍执行 `1` 次并将变量递增至 `11`。
运行 PHP 8.4 while、do...while 边界条件与 break/continue 实验
展开查看收起
<?php
declare(strict_types=1);
$cases = [];
// Case 1: while loop 1..5
$i = 1;
$whileLines = [];
while ($i <= 5) {
$whileLines[] = 'The number is ' . $i;
$i++;
}
$c1Actual = ['lines' => $whileLines, 'final_i' => $i];
$c1Expected = [
'lines' => [
'The number is 1',
'The number is 2',
'The number is 3',
'The number is 4',
'The number is 5',
],
'final_i' => 6,
];
$cases[] = [
'name' => 'while_count_1_to_5',
'passed' => $c1Actual === $c1Expected,
'expected' => $c1Expected,
'actual' => $c1Actual,
];
// Case 2: do...while loop incrementing before output (produces 2..6)
$j = 1;
$doLines = [];
do {
$j++;
$doLines[] = 'The number is ' . $j;
} while ($j <= 5);
$c2Actual = ['lines' => $doLines, 'final_j' => $j];
$c2Expected = [
'lines' => [
'The number is 2',
'The number is 3',
'The number is 4',
'The number is 5',
'The number is 6',
],
'final_j' => 6,
];
$cases[] = [
'name' => 'do_while_increment_before_output',
'passed' => $c2Actual === $c2Expected,
'expected' => $c2Expected,
'actual' => $c2Actual,
];
// Case 3: initially false condition (while runs 0 times, do...while runs once)
$wCount = 0;
$wVal = 10;
while ($wVal <= 5) {
$wCount++;
$wVal++;
}
$dCount = 0;
$dVal = 10;
do {
$dCount++;
$dVal++;
} while ($dVal <= 5);
$c3Actual = [
'while_runs' => $wCount,
'do_while_runs' => $dCount,
'do_while_final_val' => $dVal,
];
$c3Expected = [
'while_runs' => 0,
'do_while_runs' => 1,
'do_while_final_val' => 11,
];
$cases[] = [
'name' => 'while_vs_do_while_false_initial',
'passed' => $c3Actual === $c3Expected,
'expected' => $c3Expected,
'actual' => $c3Actual,
];
// Case 4: break and continue inside while loop
$n = 0;
$oddsBeforeSeven = [];
while ($n < 10) {
$n++;
if ($n === 7) {
break;
}
if ($n % 2 === 0) {
continue;
}
$oddsBeforeSeven[] = $n;
}
$c4Actual = ['odds' => $oddsBeforeSeven, 'stopped_at' => $n];
$c4Expected = ['odds' => [1, 3, 5], 'stopped_at' => 7];
$cases[] = [
'name' => 'loop_break_and_continue',
'passed' => $c4Actual === $c4Expected,
'expected' => $c4Expected,
'actual' => $c4Actual,
];
echo json_encode(['version' => PHP_VERSION, 'cases' => $cases], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n";
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
本地运行
解压后按 README 说明在对应本地环境运行脚本。
本章附件与实例包
常见错误与边界检查
- 实际端口、文档资源与应用配置都应和测试条件一致。
技术规范与官方资料
练习与参考解答
查看参考解答
while 先判断 10 <= 5 为假,执行 0 次;do...while 先执行再判断,执行 1 次。示例中先执行了 $i++ 再输出,最后一次进入时 $i=5,自增成 6 并输出后检查 6 <= 5 为假才结束,因此输出 2 到 6。