I’ve seen this convention pretty much everywhere, and, at times, it comes close to driving me nuts:
<?php //The loop ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; // end of the loop. ?>
Where the <?php
and closing ?>
are on every single line, even if there is no intervening HTML code.
My question is: Why? Why include all these extra tags?
To me, it seems this convention adds a significant amount of clutter to code, is annoying to follow in the first place, and adds that many more places to accidentally leave out an opening or closing tag.
NOTE
This is code pulled from the Twenty-Twelve theme, the example given by WordPress.
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.