Difference between if () { } and if () : endif;


Question

Are there any differences between...

if ($value) {

}

...and...

if ($value):

endif;

?

1
151
1/4/2017 5:24:39 PM

Accepted Answer

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; ?>
168
4/17/2014 11:37:29 PM

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.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon