dongxian7471 2013-05-07 08:32 采纳率: 100%
浏览 42
已采纳

在PHP中,即使在短条件语句中也应该使用花括号?

In PHP, I do know curly brackets are not required for one-statement conditionals. But this question is about good coding style. (Of course when talking about style, it's usually better to just use whichever style is consistent within the project, but let's ignore that for this question.)

So: Is it better form to enclose any conditional statements in curly brackets, or is it better (e.g. more clean) to not use brackets in simple conditions:

E.g. this:

if (!file_exists($templatefile)) {
   throw new Exception('Template file does not exist');
}

or this:

if (!file_exists($templatefile))
    throw new Exception('Template file does not exist');

Are there any common consistent good practices for this, or is it just up to personal preferences?

  • 写回答

3条回答 默认 最新

  • duanlumei5941 2013-05-07 08:34
    关注

    I have seen way too many mistakes by people who do not use the curly brackets and then, later on, add an additional command and forget to wrap it.

    My attitude is to wrap it for readability and future extensibility.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探