PHP 的 match 表达式
等级 4163 次按键
使用 PHP 8 的 match(true) 根据分数范围返回等级;85 得到 B。
PHP 代码
1function getGrade(int $score): string { // match(true) 选择第一个条件为 true 的分支2 return match(true) { // 分支从上到下检查,所以顺序很重要3 $score >= 90 => "A", // 85 不满足 >= 90 但满足 >= 80,因此这里返回 B4 $score >= 80 => "B",5 $score >= 70 => "C", // 没有条件匹配(低于 70)时使用 default6 default => "F"7 };8}9 // 输出“B”10echo getGrade(85);运行结果
B
可以用这段代码练习打字。成绩不会计入排行榜。
这段代码中的符号
这段 PHP 代码中需要输入的符号及其出现次数。
"× 8=× 7>× 7$× 4,× 3(× 3)× 3;× 2{× 2}× 2