if-else 语句语法偏好 [英] if-else statement syntax preference

查看:41
本文介绍了if-else 语句语法偏好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 if 语句的以下 2 种语法之间是否有任何性能差异(其他循环类似 - while、for、foreach 等):

I was wondering if there is any performance difference between the following 2 syntax of if-statement(similarly for other loops - while, for, foreach etc) :

 if(..) {...}
 else {...}

if(..) : ...
else : ... 
endif;

使用一种比另一种有什么优势吗?

Any advantages of using one over the other?

推荐答案

您通常在视图(模板)中使用替代语法 (if (condition): endif;)) 因为在 HTML 中更容易阅读.例如:

You normally use the alternative syntax (if (condition): endif;) in views (templates) as it's easier to read amongst HTML. For example:

<?php foreach ($items as $item): ?>
<article class="item">
  <h1><?php echo $item->title; ?></h1>
</article>
<?php endforeach; ?>

比:

<?php foreach ($items as $item) { ?>
<article class="item">
  <h1><?php echo $item->title; ?></h1>
</article>
<?php } ?>

特别是当你添加更多的控制结构,比如iffor等.如果你在HTML中使用大括号,就很难区分右大括号属于哪个哪个左花括号,而使用您知道的 endif 替代语法,您正在寻找一个开放的 if: 语句.

Especially as you add more control structures such as if, for etc. If you use curly braces in HTML, it becomes harder to distinguish which closing curly brace belongs to which opening curly brace, whereas using the alternative syntax you know with endif you're looking for an opening if: statement.

希望有所帮助.

这篇关于if-else 语句语法偏好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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