20

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?

Lucas
  • 16,930
  • 31
  • 110
  • 182
  • 4
    flip a coin, its personal choice –  May 03 '12 at 05:49
  • 3
    I find #1 more handy since it is closer to JavaScript syntax and I'm usually mixing the 2. –  May 03 '12 at 05:51
  • i tend to use both in my coding, and it causes me no trouble, since I indent my code effectively, allowing me to see divisions in my code. –  Apr 10 '15 at 00:45
  • IMO the `endif;` syntax is more verbose (i.e. it's easier to tell between different `end-something`-s that between braces), as well as it avoids brace style debates (whether to place the opening brace on a new line), ensuring consistent formatting. – Kotauskas Aug 24 '19 at 18:36

5 Answers5

27

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; ?>
Andrew
  • 227,796
  • 193
  • 515
  • 708
  • 10
    While I'm not going to knock anyone for not having a full-fledged IDE, there are certainly several editors that support code folding (and block identification) available at no cost. – Ignacio Vazquez-Abrams May 03 '12 at 06:05
  • 7
    If your function is so long that you can not associate an ending brace with an opening one, then braces vs alt syntax is the least of your problems. – Mark Kaplun Mar 15 '16 at 10:35
7

They are both exactly equivalent. Consult your organization's style guide to determine which you should use.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Yes, but it seems to me that one might be less compatible or less faster. Obviously, I might be wrong. – Lucas May 03 '12 at 06:05
  • 7
    If you're running a version of PHP that doesn't support the "alternative" syntax then you have **bigger problems** than having to convert your code... – Ignacio Vazquez-Abrams May 03 '12 at 06:06
4

This alternative syntax is in no way different than the, perhaps, more familiar syntax. Just don't mix the two. Similar syntax exists for while, for, foreach, and switch.

Typically you should choose your preferred syntax based upon readability, and your teams preference.

What really might throw you is when you begin to see "if statements" which misuse logical conjunction operators like the following:

isset( $value ) AND print( $value );
Sampson
  • 265,109
  • 74
  • 539
  • 565
0

As I've seen until now, in my app, there is no difference between them, but you can have a lot of problem if you'll mixt them without a rule because it's possible to get some errors and you will search a curly but you have not used curly. Choose one syntax an use exclusive.

0

The only difference is seen in very large documents and projects. My company uses braces rather then endif, endelse... as we work on very large projects. When you compare file sizes there is a benefit to using braces. Smaller files load faster so we work to reduce every byte we can. We comment out the end brace to make it easier to identify on testing and then delete all comments for production.

James
  • 702
  • 2
  • 15
  • 39
  • How big a single php file can be? Few mb(s)? It doesnt make any difference of the php file size on file loading, as it will be in micro seconds only. Rather, I would say if you are working with large files, it will be more easy to identify which condition or loop closed using colon keywords within large nested logic. – Saurin Dashadia May 25 '22 at 04:29