在 PHP 中堆叠多个三元运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5235632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Stacking Multiple Ternary Operators in PHP
提问by Mac Taylor
This is what I wrote :
这是我写的:
$Myprovince = (
($province == 6) ? "city-1" :
($province == 7) ? "city-2" :
($province == 8) ? "city-3" :
($province == 30) ? "city-4" : "out of borders"
);
But for every field I got the value city-4
. I want to use ternary operators instead of switch/if
because I want to experiment and see how it would be done.
但是对于每个字段,我都得到了值city-4
。我想使用三元运算符而不是switch/if
因为我想进行实验并看看它是如何完成的。
What's the problem with this code?
这段代码有什么问题?
回答by codaddict
Others have already suggested the right way of doing it but if you really want to use ternary operator you need to use parenthesis as:
其他人已经提出了正确的做法,但如果你真的想使用三元运算符,你需要使用括号作为:
$province = 7;
$Myprovince = (
($province == 6) ? "city-1" :
(($province == 7) ? "city-2" :
(($province == 8) ? "city-3" :
(($province == 30) ? "city-4" : "out of borders")))
);
回答by Felix Kling
The ternary operator is evaluated from left to right. So if you don't group the expressions properly, you will get an unexpected result.
三元运算符从左到右计算。所以如果你没有正确地对表达式进行分组,你会得到一个意想不到的结果。
It is recommended that you avoid "stacking" ternary expressions. PHP's behaviour when using more than one ternary operator within a single statement is non-obvious.
建议您避免“堆叠”三元表达式。PHP 在一条语句中使用多个三元运算符时的行为并不明显。
Your code actually is evaluated as:
您的代码实际上被评估为:
(
(
(
$province == 6 ? "city-1" : $province == 7
) ? "city-2" :
$province == 8
) ? "city-3" : $province == 30
) ? "city-4" : "out of borders";
where it should be
它应该在哪里
$province == 6 ? "city-1" : (
$province == 7 ? "city-2" : (
$province == 8 ? "city-3" : (
$province == 30 ? "city-4" : "out of borders"
)
)
);
This code might look fine but someone will read it and they will need more time than they should to understand what this code is doing.
这段代码可能看起来不错,但有人会阅读它,他们需要更多的时间来理解这段代码在做什么。
You would be better off with something like this:
像这样的事情你会更好:
$map = array( 6 = >'city-1',
7 => 'city-2',
8 => 'city-3',
30 => 'city-4');
$Myprovince = "out of borders";
if(array_key_exists($province, $map)) {
$Myprovince = $map[$province];
}
Or as @Jonahmentioned in his comment:
或者正如@Jonah在他的评论中提到的那样:
$Myprovince = isset($map[$province]) ? $map[$province] : 'out of borders';
回答by Marc B
Don't abuse the ternary operator for that sort of thing. It makes debugging near impossible to follow. Why not do something like
不要为了那种事情滥用三元运算符。它使调试几乎不可能进行。为什么不做类似的事情
switch($province) {
case 6: $Myprovince = "city-1"; break;
case 7: ...
}
or simply some chained if/then/else
或者只是一些链接的 if/then/else
if ($province == 6) {
$Myprovince = "city-1";
} elseif ($province = ...) {
...
}
回答by arnorhs
Some people have suggested using a switch statement or an if/else statement. But I would use an array instead, to make it easier to maintain and easier to read:
有些人建议使用 switch 语句或 if/else 语句。但我会改用数组,以使其更易于维护和阅读:
$provinces = array (
6 => 'city-1',
7 => 'city-2',
8 => 'city-3',
30 => 'city-4'
);
// then you can call:
$Myprovince = isset($provinces[$province]) ? $provinces[$province] : 'out of borders';
Why?
为什么?
The code will probably eventually be easier to manage. Maybe you'll want to add those province-to-city mappings from database one day.. etc.. That will be hard to maintain with a bunch of switch/case statements.
代码最终可能会更容易管理。也许有一天你会想从数据库中添加那些省到城市的映射……等等。这将很难用一堆 switch/case 语句来维护。
回答by Noah
I understand this is a question about PHP, but since this is just an educational exercise anyways I thought you might be interested in learning that Ruby and Javascript actually behave the way you expect.
我知道这是一个关于 PHP 的问题,但由于这只是一个教育练习,我认为您可能有兴趣了解 Ruby 和 Javascript 实际上的行为方式是否符合您的预期。
Ruby:
红宝石:
ree-1.8.7-2012.02 :009 > def foo x
ree-1.8.7-2012.02 :010?> x == 1 ? "city 1" : x == 2 ? "city 2" : "out of borders"
ree-1.8.7-2012.02 :011?> end
=> nil
ree-1.8.7-2012.02 :012 > foo 1
=> "city 1"
ree-1.8.7-2012.02 :013 > foo 2
=> "city 2"
ree-1.8.7-2012.02 :014 > foo 3
=> "out of borders"
Javascript:
Javascript:
> function f(x) { return x == 1 ? "city 1" : x == 2 ? "city 2" : "out of borders"; }
undefined
> f(1)
"city 1"
> f(2)
"city 2"
> f(3)
"out of borders"
回答by krtek
Try with some more parenthesis :
尝试使用更多括号:
$Myprovince = (
($province == 6) ? "city-1" :
(($province == 7) ? "city-2" :
(($province == 8) ? "city-3" :
(($province == 30) ? "city-4" : "out of borders"
))));
Your code has a problem with the ternary operator priority.
您的代码在三元运算符优先级方面存在问题。
But I think you should really drop this operator and try to use a switch
instead.
但我认为你真的应该放弃这个操作符并尝试使用 aswitch
来代替。
回答by Jonah
Use switch instead. Ternary operators really shouldn't be used for more than single conditions, as they quickly become very difficult to understand.
改用开关。三元运算符真的不应该用于多个条件,因为它们很快变得非常难以理解。
switch ($province) {
case 6:
$Myprovince = 'city-1';
break;
case 7:
$Myprovince = 'city-2';
break;
case 8:
$Myprovince = 'city-3';
break;
case 30:
$Myprovince = 'city-4';
break;
default:
$Myprovince = 'out of borders';
}
回答by Fandi Susanto
I got myself into the same problem today. The others already giving acceptable solutions. Mine just an emphasis to one liner ifs. More readable in my opinion.
我今天遇到了同样的问题。其他人已经给出了可接受的解决方案。我只是强调一个班轮如果。在我看来更具可读性。
if ($province == 6) $Myprovince = 'city-1';
elseif ($province == 7) $Myprovince = 'city-2';
elseif ($province == 8) $Myprovince = 'city-3';
elseif ($province == 30) $Myprovince = 'city-4';
else $Myprovince = 'out of borders';