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

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

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了