开发者

Is ternary operator possible here?

I'm trying to set $value1, $value2, $value3 entirely using ternary operators. This is what it looks like without ternary operators:

if ($chk1 == 20) {
   $value1 = true;                       
   if ($chk2 == 40) {
      $value2  = 100 ;
      $value3 = 300;
   } else {
      $value2 = 200 ;
      $value3 = 400;
   }
} else {
   $value开发者_如何学运维1 = false;
}

I can set $value2 and $value3, but not sure how to set $value1. Can it be done?

if ($chk1 == 20) {
   $value1 = true;                       
   $value2 = ($chk2 == 40) ? 100 : 200;
   $value3 = ($chk2 == 40) ? 300 : 400;
} else {
   $value1 = false;
}


This is how you can do it using ternary operators:

$value1 = ($chk1 == 20) ? true : false;

However, that's unnecessary here, you could just do:

$value1 = ($chk1 == 20);

Also, if you'd drop the if ($chk1 == 20) and you want $value2 and $value3 to be set only if $chk1 == 20, you'd need to change them as well, as otherwise they would be set even if $chk1 != 20 which is different from the initial code you posted.

For example, you could do:

$value2 = ($chk1 == 20) ? (($chk2 == 40) ? 100 : 200) : $value2;
$value3 = ($chk1 == 20) ? (($chk2 == 40) ? 300 : 400) : $value3;

Which should work (don't have where to test PHP right now), leaving value2 and value3 unchanged if $chk1 == 20.

However, I'd leave it at the ifs for readability, since this kind of ternary operator usage is, well, uncommon to say the least.


Alright, I'll explain then.

The behavior for $value1 is well-defined. If $chk1 equals 20 then it's true, otherwise it's false.

The behavior for $value2 and $value3 however is not quite as simple. If $chk1 is 20 then they are given values based on $chk2. However, if $chk1 is not 20 then they are undefined. The ternary operator has no concept of "undefined"; it only understands "true" and "false". Therefore, this entire system cannot be replaced by only a series of ternary operations, and the if statement checking the value of $chk1 must exist.

And this doesn't even touch upon the issues that exist if $chk2 was actually a function call with side effects.


The ternary operator syntax is

  <condition> ? <returned-value-if-true> : <returned-value-if-false>

for instance

  $value = $a < $b ? 10 : 20;

$value takes 10 if ($a < $b), otherwise it receives 20.

In your case

  $value1 = ($chk1 == 20 ? true : false);

using the ternary operator.

edit the whole program. Provided that $value2/3 do not get assigned if $chk1 != 20
Using only the ternary operator

  $value1 = ($chk1 == 20 ? true : false);
  $value2 = ($chk1 == 20 ? ($chk2 == 40 ? 100 : 200) : $value2);
  $value3 = ($chk1 == 20 ? ($chk2 == 40 ? 300 : 400) : $value3);


If you're trying to reduce the readability of the code, then do this:

if ($value1 = ($chk1 == 20)) {     
   $value2 = ($chk2 == 40) ? 100 : 200;
   $value3 = ($chk2 == 40) ? 300 : 400;
}


What are you doing?

$value1=($chk1==20);

No need for ternary. Just take the boolean value out of ($chk1==20).

If you wanted to use ternary, you could as well, just as you did with the other values, but it would be pointless.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜