9

Both these syntaxes are correct and equivalent:

With brackets:

foreach ($listOfThings as $thing) {
  echo $thing;
}

Without brackets:

foreach ($listOfThings as $thing):
  echo $thing;
endforeach;

Two questions come up in mind:

  • Is there any real difference between them or are they totally identical (performence, for example)?

  • Is it better always to use the same one or to use both, depending of some context (for example: to recognize easely the code concerning the framework).

I suspect all that to be a matter of taste and company standards, but I'm not sure of that.

4
  • related: programmers.stackexchange.com/questions/53274/…
    – Matt Ellen
    Commented Jul 7, 2011 at 15:02
  • 4
    I needed to edit a PHP app that I created a few years ago. At the time, it felt like a good idea to omit brackets where possible. It wasn't. Editing it was horrible!
    – Mike
    Commented Jul 7, 2011 at 15:23
  • 4
    Consistency is key, I could care less what you prefer but if I have to edit your code please remain consistent!!!
    – Chris
    Commented Jul 7, 2011 at 15:29
  • 1
    I personally use brackets for anything that's only php, but if I want something inside of the if/for/while loop that isn't php (javascript or html), I'll use the : + end(if/for/while).
    – dkuntz2
    Commented Oct 8, 2011 at 20:06

6 Answers 6

27

You should always use brackets. You'll run across code (and even some frameworks!) that do no-brackets on short loops or ifs but when it comes down to it, there is no better way to make your code confusing and hard to read than by leaving off the brackets. There's also no significant performance hit either way, so far as I know.

Those two extra keys can mean a world of difference to somebody reading your code.

12
  • 2
    This can be said for most programming languages like C++, Java, C# too. Use the brackets for readability, even if they are not necessarily needed.
    – Bart
    Commented Jul 7, 2011 at 14:36
  • 4
    -1 for "You should always use brackets." You cannot use brackets if you want HTML code to be generated within the loop for example. And it would be bad to use echo to output large amounts of HTML code in the loop.
    – Cracker
    Commented Jul 7, 2011 at 14:42
  • 9
    @Cracker I use brackets all the time when generating HTML inside loops and other control structures.
    – Kenneth
    Commented Jul 7, 2011 at 14:49
  • 11
    @Cracker That is the most wrong thing I have read today. Commented Jul 7, 2011 at 14:51
  • 8
    I disagree. Use of the endif; and similar constructs are better for large amounts of HTML as they are clear as to what they signify.
    – Matt Ellen
    Commented Jul 7, 2011 at 15:28
13

My opinion is that using brackets is better when you're just dealing with PHP code.

I like the 'without brackets' method when you are using a foreach or another type of block (if, while, etc.) and there is HTML output or something else in there. This is because there can be a lot of code in the middle there and it makes it clear what is being ended. Because of this, this style of coding has become the standard among Wordpress theme developers. (For examples of this, look through the code here: The Wordpress Loop).

Note: This question is different from the question of "Should I use brackets for single-line blocks." Most languages don't have another way of delimiting multi-line blocks. In most languages, there is only one way (eg. curly braces) for multiline blocks.

Here is a simple example of where I like to use the 'without brackets' approach:

<?php foreach ($listeOfThings as $thing): ?>
    <li><?= $thing ?></li>
<?php endforeach; ?>
0
5

I appear to be in the minority here, but i prefer endforeach, endif etc over braces. They clearly specify which statement they're intended to close, unlike braces (which just close the closest still-open block).

<? if (someCondition): ?>
    imagine a bunch of code here
    <? foreach ($rows as $row): ?>
       imagine a bunch more code here
    <? endforeach ?>    (without this, PHP complains about "unexpected T_ENDIF")
    and a bunch more here
<? endif ?>

<? if (someCondition) { ?>
    imagine a bunch of code here
    <? foreach ($rows as $row) { ?>
        imagine a bunch more code here
    <? } ?>             (without this, PHP complains about "unexpected $end")
    and a bunch more here
<? } ?>

In the first example, the error is pretty much confined to the if block (and whatever is inside of it), and PHP chokes on it sooner (and thus, closer to the real error). In the second, PHP gets all the way to the end of the script before realizing the braces don't match up. That's potentially a whole lot more code to look for a missing brace in.

The people preferring braces are all like "well, if you indent your code properly, braces are just as readable". The thing is, though, there are a couple of issues:

  • Not everyone is as religious about code formatting as you. Particularly that new guy that's fixing your code while you're on vacation.
  • Braces are not "just as readable". Period. I don't care how much you like C, C#, Java, or whatever other language taught you that braces were a good thing. The fact is, there are far better syntaxes for control structures -- and PHP actually includes one.
    • Braces are punctuation. A single char of punctuation. Often surrounded by other punctuation, at that. They're easy to miss. It seems to me like the brain actually tries to filter out punctuation half the time.
    • I don't know about your editor, but mine highlights keywords. They stand out, and are longer, and are thus easier to spot. It highlights braces as well, and even attempts to highlight the matching brace...but no matter how brightly you color your braces, they're not going to stand out as well as a whole highlighted word.
    • Braces just say "close something". They don't say what to close. They simply can't convey that info on their own. They rely on you to always (ALWAYS!) indent "properly", and savagely beat any programmer that doesn't (or uses spaces instead of tabs, or tabs instead of spaces, or whatever your pet coding style is). That, or you end up adding comments like // end if. And if you ever find yourself adding comments like that...why in the hell would you avoid a keyword that does the exact same thing, but can actually be verified by the interpreter?
1
  • I use brackets everytime, since I'm used to it from other languages. However, i second that there's a lot of code where you find comments(!!) nearby the brackets that describe, what the bracket is actually closing. So if there's a language construct that helps you get rid of the comment, it's absolutely worth considering it Commented Oct 14, 2015 at 8:31
1

I prefer using brackets all the times. After several millions of lines in various languages I believe that not using them may lead to errors. The most common mistake occurs when you add a line to be executed in the block and forget to add the brackets. I made this mistake myself in an if clause and I was lucky enough to spot it on the first run. I never returned to not using brackets since.

1
  • Kinda answering the wrong question here. The question isn't whether blocks are good (they are, i agree), but which syntax to use to mark the beginnings and ends of those blocks.
    – cHao
    Commented Oct 7, 2011 at 19:39
0

Always use brackets.

Joel wrote an article about making "wrong" code look wrong. Technically, there's nothing wrong about omitting the brackets. He mentioned this exact point only briefly... the idea was that not using a bracket in those scenarios where it's possible could lead to future bugs. For example, assume you don't use brackets and someone comes along hoping to add a 2nd statement to the loop, but forgets brackets, he'd introduce a bug.

http://www.joelonsoftware.com/articles/Wrong.html

2
  • 4
    I agree with this, however this question isn't asking about whether or not to use them for single-line blocks. It's asking about when to use curly braces to delimit the block or the PHP-specific : endblock; style delimiters.
    – dontangg
    Commented Jul 7, 2011 at 15:22
  • This is not the same issue as Joel is covering. this is about {...} vs <if/while/etc.>:...end<if/while/etc.>; it is a PHP specific construct.
    – Matt Ellen
    Commented Jul 7, 2011 at 15:23
-1

The without brackets syntax is good for placing HTML code in between the loop as stated in dontangg's answer. It makes the code more legible.

10
  • Wrong, wrong, wrong. You can do exactly what you just posted (except replace <?= $listeOfThings with <?= $thing. Commented Jul 7, 2011 at 14:54
  • 3
    Yes you can. However, there's nothing worse than finding in some outsourced code, 5200-lines into the file: </td><? } ?></td>
    – Bryan B
    Commented Jul 7, 2011 at 14:56
  • -1 because the syntax you labeled unusable runs just fine (except <?= $listeOfThings ?> should be <?= $thing ?>.
    – Kenneth
    Commented Jul 7, 2011 at 14:58
  • @insta if you properly indent and manage your code having the ending bracket is quite easy to read and makes it quite helpful to know where the end of the block is.
    – Kenneth
    Commented Jul 7, 2011 at 15:00
  • 3
    @Kenneth, on the other hand, having endforeach is much clearer
    – Matt Ellen
    Commented Jul 7, 2011 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.