蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行 PHP 8.4 while、do...while 边界条件与 break/continue 实验

PHPWindows x64、PHP 8.4 本地 CLI 环境。explore-looping.php
explore-looping.php109 行
<?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";
本例需在 Windows x64、PHP 8.4 本地 CLI 环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、PHP 8.4 本地 CLI 环境。

入口文件:projects/php-looping-lab/explore-looping.php

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-looping.php,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、PHP 8.4 本地 CLI 环境。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:PHP

课程关联工程包(登记关联95章) · 含当前章节或实例 · mbskycom202612a1232.zip

下载课程关联工程包:PHP

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1260.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习