Wordpress - Why have <?php and ?> on every line

This is not recommended in any WordPress style guide, and I think it is a bad coding style. Beginners are using this style, maybe because it feels more like HTML

Unfortunately, the default themes are using this style way too often, so some beginners might think it is part of a code style.

One disadvantage of this style is comment handling. Look closely at the following example and how it doesn't do what the author might expect:

<?php echo 'Important: '; // announcement ?>
<?php echo ' enter the word '; /* start ?>
<?php echo '<b>password</b>'; /* the end */ ?>

Good luck debugging that. :)

Rule: Switch between PHP and HTML context only if you have to create output in of both languages. Use regular line breaks in all other cases.

Update, further thoughts: Every valid HTML file is a complete and valid PHP program. Yes, even if it does not contain a single line of actual PHP code.

If you start from HTML and add small pieces of PHP step by step … you might end up with the style we’re discussing here. That’s where refactoring comes into the game: Once everything runs as expected, rewrite the code until it is as readable as possible, easy to maintain and to extend, without repeating parts.

I guess some people are happy without this last step, and that’s why this won’t die soon.


While I avoid this for PHP comments, I am an avid PHP opener/closer in template files. The alternative involves echoing HTML via PHP strings, which looks even worse in my opinion. As a primitive example:

<!-- Example 1 -->
<ul>
    <?php
        foreach ( $list_items as $list_item ) {
            echo "<li><a href='" . $list_item->url . "'>" . $list_item->name . "</a></li>";
        }
    ?>
</ul>

<!-- Example 2 -->
<ul>
    <?php foreach ( $list_items as $list_item ) : ?>
        <li>
            <a href="<?php echo $list_item->url; ?>">
                <?php echo $list_item->name; ?>
            </a>
        </li>
    <?php endforeach; ?>
</ul>

Is example 2 more verbose? Perhaps? But easier to read and edit, in my opinion. You can imagine how ugly it can get for complex HTML.

Also, just as a side note: using endforeach and endif when writing HTML between your PHP logic enhances readability a ton compared to }.


This is about choice between seeing page as:

  • as completely PHP-generated entity
  • as HTML document template, powered by PHP template tags

Different people tend to think about it differently. Note that functions rarely use this style, because they feel more like block of pure PHP. On other hand it's not uncommon in templates because they are more spread across files and amount of pure HTML can be easily more than that of PHP in them.

If you look at templating engines (Mustache, Twig, etc) - they look very much like this style except that their syntax tends to eliminate verbosity of plain PHP.

PS I want to note that I am talking about sane embedding of PHP in HTML, not literally opening and ending tags on every line just for the sake of it.