doufangmu9087 2019-07-04 09:55
浏览 78
已采纳

根据PSR-2,我可以在单行中使用if语句吗?

I need to know if I'am allowed to use single row if-statements according to the PSR-2 Coding Style.

I've already read the docs but i couldn't find any information about that.

https://www.php-fig.org/psr/psr-1/

https://www.php-fig.org/psr/psr-2/

<?php

// This is fine
if ($expr)
{ 
    echo $expr;
}

// This also?
if ($expr) { echo $expr; }

?>
  • 写回答

1条回答 默认 最新

  • drqxfmfa804578 2019-07-04 09:58
    关注

    no, there is clearly said:

    "An if structure looks like the following. Note the placement of parentheses, spaces, and braces; and that else and elseif are on the same line as the closing brace from the earlier body."

    https://www.php-fig.org/psr/psr-2/#51-if-elseif-else

    So 1st variant is OK, 2nd isn't.

    P.S Actually 2nd case look OK in your example, but in case of bigger conditions, in real life it is less readable, so we need to obey singular 1st approach

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!