views:

76

answers:

2

Why is there different syntax same outcome?

For example

# Example 1
if($myCondition == true) :
    #my code here
endif;

if($myCondition == true) {
    #my code here
}

# Example 2
foreach($films as $film) : 
    #my code here
endforeach;

foreach($films as $film) { 
    #my code here
}

Also I have been using <?= for ages now and I now understand that is deprecated and I should be using <?php echo Is this the case and why? It's a lot more annoying to have to write that out each time.

What are your thoughts?

+1  A: 

The colon endif, endforeach, etc syntax is known as Alternative Syntax. I can't say for certain why this functionality exists, just that it does and is supported. I can say that I've noticed the alternative syntax used more for templating purposes where it's easy to pick out an endif/endforeach than it is a closing curly-brace in the middle of HTML markup.

The <?= is known as the short open tag. You can probably find all the info you need about its use here http://stackoverflow.com/questions/200640/are-php-short-tags-acceptable-to-use

Mike B
+1  A: 

Why should the outcome be different? The one without the brackets is called alternative syntax for control structures and is very useful, e.g. when dealing with HTML.

<?php echo is much more portable because short open tags can be disabled and are disabled by default since PHP 5.3

Felix Kling
disabled by default, but is it still supported or is it deprecated?
Lizard
@Lizard it is deprecated. But it won't be removed. See the links given in http://stackoverflow.com/questions/2413661/php6-is-short-open-tag-removed-or-deprecated-or-neither
Gordon