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

They are indeed both the same, functionally.

But if the endif is getting too far from the correspondent if I think it's much better practice to give a referencing comment to it. Just so you can easily find where it was open. No matter what language it is:

if (my_horn_is_red or her_umbrella_is_yellow)
{

    // ...

    // let's pretend this is a lot of code in the middle

    foreach (day in week) {
        sing(a_different_song[day]);
    }

    // ...

} //if my_horn_is_red

That actually applies to any analogous "closing thing"! ;)

Also, in general, editors deal better with curly brackets, in the sense they can point you to where it was open. But even that doesn't make the descriptive comments any less valid.


At our company, the preferred way for handling HTML is:

<? if($condition) { ?>
   HTML content here
<? } else { ?>
   Other HTML content here
<? } ?>

In the end, it really is a matter of choosing one and sticking with it.


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; ?>

Tags:

Php

Syntax