<?php
declare(strict_types=1);
$cases = [];
// Case 1: switch multi-branch matching + default
$pickColor = static function (string $favcolor): string {
switch ($favcolor) {
case 'red':
return '你喜欢的颜色是红色!';
case 'blue':
return '你喜欢的颜色是蓝色!';
case 'green':
return '你喜欢的颜色是绿色!';
default:
return '你喜欢的颜色不是 红, 蓝, 或绿色!';
}
};
$c1Actual = [
'red' => $pickColor('red'),
'blue' => $pickColor('blue'),
'green' => $pickColor('green'),
'yellow' => $pickColor('yellow'),
];
$c1Expected = [
'red' => '你喜欢的颜色是红色!',
'blue' => '你喜欢的颜色是蓝色!',
'green' => '你喜欢的颜色是绿色!',
'yellow' => '你喜欢的颜色不是 红, 蓝, 或绿色!',
];
$cases[] = [
'name' => 'switch_color_match_and_default',
'passed' => $c1Actual === $c1Expected,
'expected' => $c1Expected,
'actual' => $c1Actual,
];
// Case 2: missing break fall-through vs intentional case grouping
$fallthrough = static function (int $step): array {
$trace = [];
switch ($step) {
case 1:
$trace[] = 'one';
case 2:
$trace[] = 'two';
break;
case 3:
$trace[] = 'three';
break;
}
return $trace;
};
$dayType = static function (string $day): string {
switch ($day) {
case 'Sat':
case 'Sun':
return 'weekend';
default:
return 'weekday';
}
};
$c2Actual = [
'fallthrough_1' => $fallthrough(1),
'fallthrough_2' => $fallthrough(2),
'sat' => $dayType('Sat'),
'mon' => $dayType('Mon'),
];
$c2Expected = [
'fallthrough_1' => ['one', 'two'],
'fallthrough_2' => ['two'],
'sat' => 'weekend',
'mon' => 'weekday',
];
$cases[] = [
'name' => 'switch_fallthrough_without_break',
'passed' => $c2Actual === $c2Expected,
'expected' => $c2Expected,
'actual' => $c2Actual,
];
// Case 3: switch loose comparison (==) vs PHP 8 match strict comparison (===)
$switchClassify = static function (mixed $val): string {
switch ($val) {
case 1:
return 'matched_int_1';
case '1':
return 'matched_str_1';
default:
return 'other';
}
};
$matchClassify = static function (mixed $val): string {
return match ($val) {
1 => 'matched_int_1',
'1' => 'matched_str_1',
default => 'other',
};
};
$unhandledCaught = false;
try {
$_ = match (99) {
1 => 'one',
};
} catch (\UnhandledMatchError $e) {
$unhandledCaught = true;
}
$c3Actual = [
'switch_str_1' => $switchClassify('1'),
'match_str_1' => $matchClassify('1'),
'match_int_1' => $matchClassify(1),
'match_unhandled_throws' => $unhandledCaught,
];
$c3Expected = [
'switch_str_1' => 'matched_int_1',
'match_str_1' => 'matched_str_1',
'match_int_1' => 'matched_int_1',
'match_unhandled_throws' => true,
];
$cases[] = [
'name' => 'switch_loose_vs_match_strict',
'passed' => $c3Actual === $c3Expected,
'expected' => $c3Expected,
'actual' => $c3Actual,
];
// Case 4: switch evaluates expression exactly once
$evalCount = 0;
$nextVal = static function () use (&$evalCount): string {
$evalCount++;
return 'green';
};
$matched = '';
switch ($nextVal()) {
case 'red':
$matched = 'r';
break;
case 'blue':
$matched = 'b';
break;
case 'green':
$matched = 'g';
break;
}
$c4Actual = ['eval_count' => $evalCount, 'matched' => $matched];
$c4Expected = ['eval_count' => 1, 'matched' => 'g'];
$cases[] = [
'name' => 'switch_expression_evaluated_once',
'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-switch-lab/explore-switch.php
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-switch.php,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、PHP 8.4 本地 CLI 环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联95章) · 含当前章节或实例 · mbskycom202612a1232.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1309.zip