简体   繁体   English

PHP三元运算符的多个语句

[英]Multiple Statements For The Ternary Operator Of PHP

I'm wondering if I could use the ternary operator for something like this: 我想知道是否可以对这样的东西使用三元运算符:

var string = "";
if (something) {
  string = "foo"
} else if (somethingElse) {
  string = "bar";
} else if (bla) {
  string = "pool";
} else if (xxxxx) {
  string = "coffee";
} else {
  string = "";
}

As far as I remember, I can do this in Java language: 据我所记得,我可以用Java语言做到这一点:

String string = something?"foo":somethingElse?"bar":bla?"pool":xxxxx?"coffee":"";

But I'm not sure about PHP, I'm not even sure if it's OK to use ternary operator in this case or not. 但是我不确定PHP,甚至不确定在这种情况下是否可以使用三元运算符。

Eg 例如

 if (something) {
      string = "foo"
    } else if (somethingElse) {
      string = "bar";
    } else if (bla) {
      string = "pool";
    } else if (xxxxx) {
      string = "coffee";
    } else {
      string = "";
}

is equivalent to in PHP 等同于PHP

(something) ? 'foo' : ((somethingElse) ? 'bar' : ((bla) ? 'pool' : ((xxxxx) ? 'coffe' : '')));

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

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