if替代语法_PHP的替代控制结构语法

if替代语法

If you've ever had the pleasure experience of digging through WordPress' code, you've probably run into PHP's alternate control structure syntax. An example of this can be seen in the following code:

如果您曾经有过愉快的研究WordPress代码的经验,那么您可能已经碰到过PHP的备用控件结构语法。 可以在以下代码中看到一个示例:


$code = 3;
if($code == 1):
	echo 'The process succeeded.';
else:
	echo 'The process failed.';
endif;


The same works with while, for, and foreach loops, as well as the switch statement:

while,for和foreach循环以及switch语句也可以使用相同的方法:


$items = array('MooTools','PHP','CSS');
foreach($items as $item):
	echo $item;
endforeach;


我应该使用这个吗? 什么时候? (Should I Use This? When?)

Whether or not to use this syntax is completely up to you -- it really depends on your coding style and what you do (or don't) consider readable. I see this syntax used most often when many control structures are used within the same block of code; the "end____" syntax makes it much more apparent which structure you're ending (as opposed to simply coding "}"). I also see this syntax used more frequently than traditional control structure syntax when PHP code is closely embedded in HTML code.

是否使用此语法完全取决于您-实际上取决于您的编码样式以及您认为(或不认为)可读的内容。 当在同一代码块中使用许多控制结构时,我会经常使用这种语法。 “ end____”语法使您更清楚要结束的结构(与简单地编码“}”相对)。 当PHP代码紧密嵌入HTML代码中时,我还看到这种语法比传统控件结构语法使用得更频繁。

你怎么看? (What Do You Think?)

I'd love to hear what you have to say about this syntax. I've begun using this alternate syntax more and tend to like it...possibly for the novelty of it. Many people have told me they hate it while others find it more readable. What do you think?

我很想听听您对这种语法的看法。 我开始更多地使用这种替代语法,并且倾向于喜欢它……可能是因为它的新颖性。 许多人告诉我他们讨厌它,而其他人则觉得它更易读。 你怎么看?

翻译自: https://davidwalsh.name/php-alternate-syntax

if替代语法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值