switch statement with multiple cases which execute the same code(具有执行相同代码的多个案例的 switch 语句)
问题描述
我有以下代码:
<?php
echo check('three');
function check($string) {
switch($string) {
case 'one' || 'two' : return 'one or two'; break;
case 'three' || 'four' : return 'three or four'; break;
}
}
目前它输出:
one or two
但显然我希望代码返回三个或四个
.
But obviously I want the code to return three or four
.
那么,为多个 case 语句返回相同代码的正确方法是什么?
So what is right method to return the same code for multiple case statements?
推荐答案
不可能.case
项必须是 VALUES.你有表达式,这意味着表达式被评估,并且该表达式的结果是将它们与 switch()
中的值进行比较.这意味着你有效地得到了
Not possible. the case
items must be VALUES. You have expressions, which means the expressions are evaluated, and the result of that expression is them compared against the value in the switch()
. That means you've effectively got
switch(...) {
case TRUE: ...
case TRUE: ...
}
您不能在一个案例中使用多个值.但是,您可以使用fallthrough support":
You cannot use multiple values in a case. YOu can, however, use the "fallthrough support":
switch(...) {
case 'one':
case 'two':
return 'one or two';
case 'three':
case 'four':
return 'three or four';
}
这篇关于具有执行相同代码的多个案例的 switch 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:具有执行相同代码的多个案例的 switch 语句


基础教程推荐
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何替换eregi() 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01