这两个foreach循环之间有什么区别?

I have seen in cakephp that foreach loop is used like this


foreach($tags as $tag) :
       \\code here
endforeach;

and I used to write this style of foreach


foreach($tags as $tag)
{
     //code here
}

what is the difference between these two foeach loops and which one is better and makes more sense to implement ?

Thanks

They are equivalent, but the second first is sometimes more readable when your PHP is embedded with HTML.

They are identical. I'd use the ones with the curly braces though, since the syntax is more like other PHP constructs, C, C++, Java, etc..

They are equivalent, but the first one sometimes is more readable when your PHP is embedded in HTML.
:)~

As of it's equality, no method can be called "better", and can be only subject of agreement.

The first one dates from an early PHP syntax (before PHP 4) and the second one is what's generally accepted now. IMHO I'd avoid using the first one and would always use curly braces, even for something like

<?php foreach ($foo as $f) { ?>
<div><?= $f ?></div>
<?php } ?>

Because many editors have brace highlighting and it's just better that way :)

They are completely identical, thus the first one is most likely to be used because it allows one to let the braces go, you don't need to remember where the braces are and what is open when adding a large part of other codes or HTML design.

Just like the alternative for IF statement:

<?php
if ($foo):
   echo "is ok
";
elseif ($bar):
   echo "not ok
";
else:
   echo "dont't know
";
endif;
?>

It comes down to personal or team coding standards. I prefer the {} option as it makes more universally readable code. True, it gets messy in HTML, but again with a coding standard you can ease that.

If you have a lot of HTML and PHP within the loop, using the : option can create serious confusion lower down in the code, especially if the loop runs off the bottom of the page. Syntax highlighting with {} will let you identify the loop contents much more easily.