why is there different syntax same outcome?
문제
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?
해결책
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 Are PHP short tags acceptable to use?
다른 팁
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