0

Can I please know the difference between

 <?php if() : ?>    
 <?php endif; ?>

and

<?php if() { ?>
<?php } ?>
animuson
  • 53,861
  • 28
  • 137
  • 147
user1074803
  • 267
  • 1
  • 4
  • 14

2 Answers2

3

this is just alternative syntax called braceless syntax

If you follow MVC design pattern then only your view files should have HTML in them to begin with. Using the braceless syntax in these files only further separates them thematically from the rest of the code.

The major advantage of braceless syntax is that braces get lost while jumping into and out of php mode, especially if you use php short tags.

xkeshav
  • 53,360
  • 44
  • 177
  • 245
  • 1
    The major issue with short tags is that dynamic xml files are very awkward given that `` is a required header. – zzzzBov Jul 31 '12 at 04:46
  • @zzz Thanks for the information: i was really not aware of this. – xkeshav Jul 31 '12 at 04:47
  • I'd say you're most likely to run into the issue when trying to generate an RSS feed. It doesn't come up that often, and you can get around it with an echo statement, but it's the reason why short tags are discouraged, but the short echo tag `= ?>` is [enabled by default in PHP 5.4](http://programmers.stackexchange.com/a/151694/7865). – zzzzBov Jul 31 '12 at 04:51
  • @zzz But i have read that from PHP 6. short tag will be allowed by default. – xkeshav Jul 31 '12 at 04:52
3

There is no difference, it's down to your preference.

Personally I use the former when inside a view, the latter everywhere else.

Inside a view:

<div>
    <p>Cake?</p>
    <?php if (true): ?>
    <p>Cake!</p>
    <?php endif; ?>
</div>

Elsewhere:

echo 'Cake?';
if (true) {
    echo 'Cake!';
}
Michael Robinson
  • 29,278
  • 12
  • 104
  • 130