what's the significant benefit of using alternative colon syntax for if, foreach, while etc in PHP?
Usual if syntax
if(){
}else{
}
Using colon
if:
else:
endif;
I am going through yii framework, and I see everywhere in the template the author has used the alterna开发者_开发知识库tive colon syntax. I was wondering if there's a significant advantage ?
One can argue about it, but IMO the alternative syntax is much easier to read when you use control structures in HTML. Especially when they are nested.
The "closing" tags (e.g. <?php endif; ?>
) are much more descriptive and they are easier to spot in HTML than <?php } ?>
.
I would not use them in "normal" code though. There it makes the code actually harder for me to read. They feel somehow out of place. But I guess this is a matter of habit.
It's just a matter of taste, really. If you come from a VB coding background, you may find it more familiar to use the colon syntax. Some people find that some code is easier to read when using Colon syntax. Use it if you find it appropriate.
The way I've seen this used the most is in templates, it seems people find it more readable. Another reason would be being used to them when coming from another language (Like as said by Andreas Visual Basic).
Other than that there's no advantage to using it, it's just a different notation.
精彩评论