Are parentheses required in PSR-2 PHP ternary syntax?

后端 未结 5 1199
春和景丽
春和景丽 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 21:06

    Common convention is always simplify. PSR standard goes a way that

    $error = $error_status ? 'Error' : 'No Error';
    

    Seems more cleaner than parentheses.

    If you want explicit more readability, the PSR-2 standard goes to:

    if ($error_status) {
        $error = 'Error';
    else {
        $error = 'No Error';
    }
    

    It's all. PSR it's a standard to better understand our code, when you write a code like you are providing, you're going deeper on simplification, and there's no limitation to your imagination, just avoid not exceed PSR rules.

    Use PHP Code Sniffer to check-out your code on PSR1 and PSR2 rules.

    Code Sniffer

提交回复
热议问题