我何时使用if / endif与If {}? [英] When do I use if/endif versus If{}?

查看:133
本文介绍了我何时使用if / endif与If {}?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那么问题就是自我解释。

Well the question is self explanatory.

在PHP中何时使用 if / endif 表示法标准 if(something){} 表示法?

In PHP when do I use the if/endif notation instead of the standard if(something){} notation?

示例:

<?php if($a == 5): ?>
A is equal to 5
<?php endif; ?>

Versus:

<?php if($a == 5){ ?>
A is equal to 5
<?php } ?>


推荐答案

其他人给出了模板化的答案,但是没有真正解释为什么。卷曲括号非常适合表示块,但它们依赖于缩进来清晰地读取。所以这很清楚:

Others have given the answer "for templating", but haven't really explained why. Curly braces are great for denoting blocks, but they kind of rely on indentation to be read clearly. So this is fairly clear:

<?php
if (1 == 2)
{
    while (1 < 2)
    {
        doSomething();
    }
}

很明显哪个大括号匹配哪个。

It's obvious which brace matches which.

但是,如果你要进入HTML块,你可能会完全停止缩进。例如:

If, however, you're going into an HTML block, you're probably going to stop indenting cleanly. For instance:

<?php
if (1 != 2) { ?>
<div>This always happens! <?php while (true) { ?>
  <p>This is going to cause an infinite loop!</p>
<?php }
}

这很难理解。如果您使用 endif 样式,它看起来会更清晰:

That's hard to follow. If you use the endif style, it looks a little cleaner:

<?php
if (1 != 2): ?>
<div>This always happens! <?php while (true): ?>
  <p>This is going to cause an infinite loop!</p>
<?php endwhile;
endif;

代码的内容更清楚地显示了正在做的事情。对于这种有限的情况,它更清晰。

The content of the code shows more clearly what's being done. For this limited case, it's more legible.

这篇关于我何时使用if / endif与If {}?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆