php 开关的奇怪行为?
我想做的事情的一个例子会更明确:
var_dump($opti_point); //int 0
if ($opti_point>=0 && $opti_point < 25) echo 'good';//echoing good
switch ($opti_point) {
case ($opti_point>= 0 && $opti_point < 25):
$test = 0;
break;
case ($opti_point >= 25 && $opti_point < 50):
echo 'we are in this case'; // This case is called !
$test = 2;
break;
default:
test = 0;
break;
}
这里有技巧吗?
谢谢
A sample of what I'm trying to do will be more explicit:
var_dump($opti_point); //int 0
if ($opti_point>=0 && $opti_point < 25) echo 'good';//echoing good
switch ($opti_point) {
case ($opti_point>= 0 && $opti_point < 25):
$test = 0;
break;
case ($opti_point >= 25 && $opti_point < 50):
echo 'we are in this case'; // This case is called !
$test = 2;
break;
default:
test = 0;
break;
}
Is there a trick here ?
thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我会和你一起验证代码,
你应该在开关中使用 TRUE
I'll go true the code with you
you should use TRUE in the switch
如果这是确切的代码然后尝试这个
var_dump($opti_point); //int 0
if this is the exact code then try this
var_dump($opti_point); //int 0
您对
switch-case
的工作原理存在误解。Case
不会测试您的表达式是否为布尔值 TRUE!它将其值与“switch”值进行比较!
这是一个解释:
$opti_point>= 0 && $opti_point < 25
求值为 true,整数表示形式为 1,并且由于PHP
可以自行处理类型,因此它会将 true 变为 1,并将其与 switch 中的值 0 进行比较$ opti_point >= 25 && $opti_point < 50
计算结果为 false,即 0 作为整数,所以...这就是你的情况;)You have a misunderstanding on how
switch-case
works.Case
DOES NOT TEST YOUR EXPRESSION TO BE boolean TRUE!It compares its value to 'switch' value!
Here is an explanation:
$opti_point>= 0 && $opti_point < 25
evalutes to true which integer representation is 1 and sincePHP
can deal with types on it's own, it turnes true to 1 and compares it with value in switch which is 0$opti_point >= 25 && $opti_point < 50
evaluates to false which is 0 as integer, so... that's your case ;)不幸的是,你不能将比较放在“案例”内......
仅当值可以具有有限数量的值之一时才使用开关,如下所示:
解决方法是使用:
哪个可以工作,但有点难看...
此外,您在 < 中缺少单引号code>echo we are in this case'; 应该是
echo 'we are in this case';
你应该使用 if 来代替 =)
You cannot put comparisons inside "case" unfortunately...
A switch is only used when a value can have one of a limited number of values like so:
A workaround would be to use:
Which will work, but is a bit ugly...
Also, you're missing a single quote in
echo we are in this case';
which should beecho 'we are in this case';
You should be using an if instead =)
如果您在这种情况下进行比较,则需要将
switch
参数更改为true
或false
。You need to change the
switch
argument totrue
offalse
if you do comparison liek that in the cases.你不是在比较你认为你在比较的东西。这是我认为你想要的代码。
在您的示例中,您正在将逻辑语句的结果...
与 $opti_point 的值
进行比较,因此 PHP 实际上是将您认为的整数转换为布尔值,以将其与条件语句的结果进行比较。
You are not comparing what you think you are comparing. This is the code I think you want.
In your example, you are comparing the result of the logical statement...
To the value of $opti_point
So PHP is actually converting what you think in an integer into a boolean to compare it with the result of the conditional statement.
我认为这是使用 switch 语句的一种非常糟糕的方式,你不应该在 case 中放置条件语句...事实上,我确信这在其他语言中是非法的,并且我不确定它是否应该起作用在 PHP 中。请改用多个串联的 if-else 条件:
I think that's a very bad way to use a switch statement, you should not put conditional sentences in the cases... In fact, I'm sure that that would be illegal in other languages and I'm not sure that it should work in PHP. Use a number of concatenated if-else conditions instead: