Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Curly braces or colon-endif statements in PHP - which one provides better performance and code compatibility?

Tags:

I was looking through many snippets of code, and I have found that people can use the following two methods in an if statement:

Method 1:

<?php
  if ($condition) {
    // Do this
  }
?>

Method 2:

<?php
  if ($condition):
    // Do this
  endif;
?>

So which method is more compatible with PHP compilers and versions with PHP, or is there no discernible difference between the two?

like image 477
Lucas Avatar asked May 03 '12 05:05

Lucas


1 Answers

Most of the time the alternative (endif) syntax is used in view scripts. It's often hard to see/notice the end of an if statement since a curly brace only takes up one character, when you're at the bottom of a file, it's hard to tell if it's the end of an if or a foreach. For example:

<?php if ($condition): ?>

    <div>a huge block of html</div>

<?php endif; ?>
like image 138
Andrew Avatar answered Sep 23 '22 03:09

Andrew