Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
    
2  
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 Jul 7 '11 at 15:23
3  
Consistency is key, I could care less what you prefer but if I have to edit your code please remain consistent!!! –  Chris Jul 7 '11 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 Oct 8 '11 at 20:06

6 Answers 6

up vote 12 down vote accepted

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's no better 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.

share|improve this answer
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 Jul 7 '11 at 14:36
3  
-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 Jul 7 '11 at 14:42
5  
@Cracker I use brackets all the time when generating HTML inside loops and other control structures. –  Kenneth Jul 7 '11 at 14:49
8  
@Cracker That is the most wrong thing I have read today. –  Jarrod Nettles Jul 7 '11 at 14:51
4  
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 Jul 7 '11 at 15:28

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; ?>
share|improve this answer
    
You use ASP-style tags when doing PHP?? –  Jarrod Nettles Jul 7 '11 at 14:35
    
lol. Whoops, sorry. I've been doing some Rails lately. I started with a ?, then I just went into autopilot. I fixed it! –  dontangg Jul 7 '11 at 14:38

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.

share|improve this answer
    
Wrong, wrong, wrong. You can do exactly what you just posted (except replace <?= $listeOfThings with <?= $thing. –  Jarrod Nettles Jul 7 '11 at 14:54
    
Yes you can. However, there's nothing worse than finding in some outsourced code, 5200-lines into the file: </td><? } ?></td> –  insta Jul 7 '11 at 14:56
    
-1 because the syntax you labeled unusable runs just fine (except <?= $listeOfThings ?> should be <?= $thing ?>. –  Kenneth Jul 7 '11 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 Jul 7 '11 at 15:00
2  
@Kenneth, on the other hand, having endforeach is much clearer –  Matt Ellen Jul 7 '11 at 15:06

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.

share|improve this answer
    
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 Oct 7 '11 at 19:39

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

share|improve this answer
3  
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 Jul 7 '11 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 Jul 7 '11 at 15:23

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?
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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