Difference between if () { } and if () : endif;
Are there any differences between...
if ($value) {
}
...and...
if ($value):
endif;
?
Solution 1:
They are the same but the second one is great if you have MVC in your code and don't want to have a lot of echos in your code. For example, in my .phtml
files (Zend Framework) I will write something like this:
<?php if($this->value): ?>
Hello
<?php elseif($this->asd): ?>
Your name is: <?= $this->name ?>
<?php else: ?>
You don't have a name.
<?php endif; ?>
Solution 2:
I personally really hate the alternate syntax. One nice thing about the braces is that most IDEs, vim, etc all have bracket highlighting. In my text editor I can double click a brace and it will highlight the whole chunk so I can see where it ends and begins very easily.
I don't know of a single editor that can highlight endif, endforeach, etc.
Solution 3:
I think this say it all:
this alternative syntax is excellent for improving legibility (for both PHP and HTML!) in situations where you have a mix of them.
http://ca3.php.net/manual/en/control-structures.alternative-syntax.php
When mixing HTML an PHP the alternative sytnax is much easier to read. In normal PHP documents the traditional syntax should be used.