开发者

difference between foreach()

I开发者_StackOverflow社区s there any major difference between

foreach(){
}

foreach():
endforeach;


No, it's just alternative syntax. They're the exact same thing.


I believe the foreach alternative ending is endforeach; Basically it's the same. The alternative syntax is there to give you the choice to decide what to use.

For example I use this in my templates:

<?php foreach (...): ?>
   .... some html here ....
<?php endforeach; ?>

and I use the traditional { } way everywhere else. It just adds some readability.

More info here: http://php.net/manual/en/control-structures.alternative-syntax.php


Yes, they have different syntax.

The comments section in Alternative syntax for control structures tries to lay out some pros and cons, but ultimately the difference is code aesthetics.

If you follow MVC design pattern then only your view files should have HTML in them to begin with. Using the braceless syntax in these files only further separates them thematically from the rest of the code.

The major advantage of braceless syntax is that braces get lost while jumping into and out of php mode, especially if you use php short tags (which contrary to what is stated elsewhere, if you are using htaccess to deploy mod_rewrite in your application it is safe to use short tags in your application. The server admins CANNOT deny short tags to you while simultaneously granting mod_rewrite (and why they would even try is beyond me).


The second form referred to is known as the Alternative syntax for control structures. It enables you to "dip in and out" of php.

In terms of performance, I would guess there's no (humanly) observable difference.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜