doufangmu9087 2019-07-04 01: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 01: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

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部