2

In most themes I've looked at and WordPress core, I'm seeing the use of endwhile and endif.

Is there a good reason why WordPress uses these instead of curly braces for block statements?

7

As in many things wordpress, inertia has a lot to do with why things are done in specific way. Early core themes were written using those constructs and later developers copied them because they either did not know better or thought there was some non obvious reason to prefer them.

As @Howdy_McGee said, it is just a style preference but I would avoid the endif, endwhile as curly braces are standard block delimiters across many languages and therefor are in general easier to read even if we will ignore the benefit of syntax highlighting and block collapsing in general editors.

| improve this answer | |
10

The best reason I can find it to separate Logic from Structure or Control from Markup. The handbook on the PHP Coding Standard for WordPress says:

Braces should always be used, even when they are not required:

if ( condition ) {
    action0();
}

if ( condition ) {
    action1();
} elseif ( condition2 ) {
    action2a();
    action2b();
}

foreach ( $items as $item ) {
    process_item( $item );
}

Note that requiring the use of braces just means that single-statement inline control structures are prohibited. You are free to use the alternative syntax for control structures (e.g. if/endif, while/endwhile) — especially in your templates where PHP code is embedded within HTML, for instance:

<?php if ( have_posts() ) : ?>

    <div class="hfeed">

        <?php while ( have_posts() ) : the_post(); ?>

            <article id="post-<?php the_ID() ?>" class="<?php post_class() ?>">
                <!-- ... -->
            </article>

        <?php endwhile; ?>

    </div>

<?php endif; ?>

Some could argue that braces are better because of syntax highlighting ( i.e. click opening bracket and it highlights closing bracket ) but it comes down to preference. The alternative syntax isn't required or necessary but offered as an alternative so you're free to use whichever is the cleanest to read.


Personally, I use the alt syntax in templates ( or any time I plan on mixing HTML and PHP ) with appropriate spacing and indentation as I find it easier to read overall.

| improve this answer | |
  • Totally agree. I'm one who prefer the use of braces because of syntax highlighting. And I can use them without breaking WordPress convention: if( condition ) { ?> <HTML> <?php }. As you said, alternative syntax of control structures is optional. – cybmeta Mar 14 '16 at 19:43
  • Alright. Yeah I use braces. I don't think either way makes for readable code when mixing with html, even a 100 lines and it starts to look like a mess to me. Most used cms in the world and I got no idea why it doesn't ship with template engine. – Larry Lawless Mar 14 '16 at 19:50
  • There was a comment a while ago on Stack Overflow which stated it is really bad practice to use curlies. I just laughed. Curlies, IMHO, are still the best to use as it helps a lot with debugging, and all code editors supports them ;-) – Pieter Goosen Mar 15 '16 at 4:10
  • Choose the other answer. This probably originated with Mullenweg – Larry Lawless Mar 15 '16 at 12:41
  • @PieterGoosen Pippin has a good argument against curlies - I understand where he's coming from there but most WP sites clients would never touch templates. It's also of course a relatively old post. – Howdy_McGee Mar 15 '16 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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