简体   繁体   English

如何在 PHP 中连接多个三元运算符?

[英]How to concatenate multiple ternary operator in PHP?

I use ternary operators alot but I can't seem to stack multiple ternary operator inside each other.我经常使用三元运算符,但我似乎无法在彼此内部堆叠多个三元运算符。

I am aware that stacking multiple ternary operator would make the code less readable but in some case I would like to do it.我知道堆叠多个三元运算符会使代码的可读性降低,但在某些情况下我想这样做。

This is what I've tried so far:这是我迄今为止尝试过的:

$foo = 1;
$bar = ( $foo == 1 ) ? "1" : ( $foo == 2 ) ? "2" : "other";
echo $bar; // display 2 instead of 1

What is the correct syntax?什么是正确的语法?

Those parenthesis are what I think is getting you.那些括号是我认为你得到的。

Try尝试

$foo = 1;
$bar = ($foo == 1) ? "1" : (($foo == 2)  ? "2" : "other");
echo $bar;

The problem is that PHP, unlike all other languages , makes the conditional operator left associative.问题是 PHP与所有其他语言不同,它使条件运算符左结合。 This breaks your code – which would be fine in other languages.这会破坏你的代码——这在其他语言中会很好。

You need to use parentheses:您需要使用括号:

$bar = $foo == 1 ? "1" : ($foo == 2 ? "2" : "other");

(Notice that I've removed the other parentheses from your code; but these were correct, just redundant.) (请注意,我已从您的代码中删除了其他括号;但这些是正确的,只是多余的。)

You need some parentheses around the right hand operand:您需要在右侧操作数周围加上一些括号:

$foo = 1;
$bar = ( $foo == 1 ) ? "1" : (( $foo == 2 ) ? "2" : "other");
echo $bar;

PHP's interpreter is broken, and treats your line: PHP 的解释器坏了,并处理你的行:

$bar = ( $foo == 1 ) ? "1" : ( $foo == 2 ) ? "2" : "other";

as作为

$bar = (( $foo == 1) ? "1" : ( $foo == 2)) ? "2" : "other";

and since that left hand expression evaluates as "true" the first operand of the remaining ternary operator ("2") is returned instead.并且由于该左手表达式的计算结果为“真”,因此返回剩余三元运算符(“2”)的第一个操作数。

You could write this correctly thus:你可以这样正确地写:

$bar = ($foo == 1) ? "1" : (($foo == 2) ? "2" : "other");

(ie: Simply embed the 'inner' ternary operator in parenthesis.) (即:只需将“内部”三元运算符嵌入括号中。)

However, I'd be really tempted not to do this, as it's about as readable as a particularly illegible thing that's been badly smudged - there's never any excuse for obfuscating code, and this borders on it.但是,我真的很想不这样做,因为它的可读性就像一个被严重弄脏的特别难以辨认的东西——从来没有任何借口可以混淆代码,而且这也差不多。

Put parenthesis around each inner ternary operator, this way operator priority is assured:在每个内部三元运算符周围加上括号,这样可以确保运算符优先级:

$bar = ( $foo == 1 ) ? "1" : (( $foo == 2 ) ? "2" : "other");

Add the parenthesis:添加括号:

$bar = ( $foo == 1 ) ? "1" : (( $foo == 2 ) ? "2" : "other");

Just stack up the parenthesis, and you've got it:把括号叠起来,你就明白了:

$bar = ($foo==1? "1" : ($foo==2? "2" : "other"));

As an aside, if you've got many clauses, you should consider using a switch :顺便说一句,如果你有很多子句,你应该考虑使用switch

switch ( $bar ) {
  case 1:  echo "1";
  case 2:  echo "2";
  default: echo "other";
}

If the switch gets long, you can wrap it in a function.如果开关变长,您可以将其包裹在 function 中。

$foo = 1;
$bar = ( $foo == 1 ) ? "1" : (( $foo == 2 ) ? "2" : "other");
echo $bar;

Just use extra ( ) and it will work只需使用额外的 ( ) 就可以了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM