Are parentheses required in PSR-2 PHP ternary syntax?

后端 未结 5 1218
春和景丽
春和景丽 2021-02-13 20:13

Question: are parentheses required in PSR-2 PHP ternary syntax?

Looking for which (if either) of the following ternary statement\'s syntax is compliant with PSR-2 - I

5条回答
  •  说谎
    说谎 (楼主)
    2021-02-13 20:58

    Some mixed opinions on this, and it's something that's unfortunately arbitrary.

    What appears more common to me from what I’ve seen and have learned from would be best explained by noting the differences below, more specifically; how the parentheses don’t move in either case.

    Long hand:

    if ($is_full_page) {
      echo "medium-6";
    } else {
      echo "medium-7";
    }
    

    Short hand:

    echo ($is_full_page) ? 'medium-6' : 'medium-7';
    

    That; to me; is true consistency in all it's beauty.

提交回复
热议问题