Can anyone tell me why most themes are using this:

<?php if ($page == 0): ?>
  <h2><a href="<?php print $node_url ?>" title="<?php print $title ?>"><?php print $title ?></a></h2>
<?php endif; ?>

instead of this:

<?php if ($page == 0) { ?>
  <h2><a href="<?php print $node_url ?>" title="<?php print $title ?>"><?php print $title ?></a></h2>
<?php } ?>

Is this just the third-party developer's personal preference, or is this style encouraged by the Drupal community?
Your thoughts...

-Chris

Comments

test127’s picture

It's probably just easier to read. At least for me, it's far easier while scanning for my eyes to pick out "endif" than to spot "}". Also, it may be a little easier to pick up on the meaning of it for some of the non-programmer users.

Chris Gillis’s picture

In case anyone is interested, from: www.drupal.org/node/318

You are strongly encouraged to always use curly braces even in situations where they are technically optional.

heine’s picture

The technically optional refers to the equivalence of

if ($foo)
  bar();
baz();

// Better:
if ($foo) {
  bar();
}
baz();

The alternative block syntax is simply much easier to read in the tagsoup that are themes. Perhaps we can create a special theme section in the code style guidelines.

--
The Manual | Troubleshooting FAQ | Tips for posting | How to report a security issue.

AdrianB’s picture

I was wondering the same thing myself. I can see the readability argument but an argument for using curly braces would be that editors like BBEdit can balance the braces and check if they're unmatched, which is a big advantage sometimes.

I tried to find any official guidance here on drupal.org, but this article was the closest I could get.