Multiple conditions in the ternary operator safe?

前端 未结 6 1577
情话喂你
情话喂你 2021-01-17 09:22

I have seen advice that says the ternary operator must not be nested.

I have tested the code below and it works okay. My question is, I haven\'t seen the ternary op

6条回答
  •  遇见更好的自我
    2021-01-17 09:56

    It's totally legal, it works and is "as reliable as if", but it looks ugly.

    If you put each ternary statement inside parenthesis, nesting would be fine too:

    $res = ( $rule1 ? true : ( $rule2 ? true : false ) )
    

    The only thing that is advised against in the manual is nesting without parenthesis like this:

    $res = ( $rule1 ? true : $rule2 ? true : false )
    

提交回复
热议问题