Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
also, shorthand while statements =o –  Tom J Nowell Jan 10 '13 at 0:08
3  
Flagging to close - there is no definitive answer to this question, and it's certainly not a WP specific issue - any person working with PHP will face this issue –  anu Jan 10 '13 at 11:16
1  
@anu: First, a question may not always have a single, definitive answer (though it still may have a best answer). The guidelines say "practical, and answerable". Second, yes, this is technically a PHP issue, but I've seen this much, much more in my short time working with WP. So, while this isn't limited to WP, it seems to me to be correlated enough to ask in a WP setting. –  Indigenuity Jan 10 '13 at 17:32

4 Answers 4

up vote 14 down vote accepted

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.

share|improve this answer
    
It's a pity that the syntax highlighting above doesn't show what actually happens with that comment... –  webaware Jan 9 '13 at 23:22
7  
@webaware I think that illustrates the problem even more. :) –  toscho Jan 9 '13 at 23:23
    
True :) (plus some chars to keep SE comment police happy) –  webaware Jan 9 '13 at 23:27
2  
@AndyAdams Fair enough, I have rephrased that. And now go away, you evil recommender. :) –  toscho Jan 10 '13 at 1:14
3  
Themes use this style because doing otherwise is just too ugly. When refactoring themes, I'd prefer to rewrite the code to be as readable as possible, and that means not having echo/printf/var_dump all around and to put each control structures inside their own pairs of <? ... ?> to make nesting easier to comprehend. One thing I'd do differently than the OP's example though, is that I'd put the_post(); on its own line. –  Lie Ryan Jan 10 '13 at 4:16

My question is: Why? Why include all these extra tags?

The answer is pretty simple: Audience. When people (not programmers) grab a theme, then FTPing up their install, running the 5min setup and so on already feels like programming to them. When they then want to add or change a single line of whatever in their theme, then they maybe already figured out what HTML is. PHP still will be far out of their reach. So my guess is that the idea behind this is to allow easier adding or removing of elements without breaking everthing when they make a mistake.

Note: This is not what I like, prefer or would recommend. It's just what I think why this happens.

share|improve this answer

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 }.

share|improve this answer
5  
the huge advantage of }over endif however is that (in many editors) you can easily see where the opening { is and therefore whether everything has been properly closed. Try figuring that out with endif and a whole bunch of conditionals... –  Piet Jan 10 '13 at 1:10
2  
foreach ( $list_items as $list_item ) printf( '<li><a href="%1$s">%2$s</a></li>', $list_item->url, $list_item->name );two lines, HTML and PHP nicely separated. :P –  toscho Jan 10 '13 at 1:18
4  
@toscho: You totally missed the point. You're still mixing PHP and HTML, people that prefers the second style does it because we want to avoid having HTML inside PHP string. I use the second style when using PHP as a templating language because it is the only way to nest a mixture of PHP and HTML sensibly, while I use the former when using PHP as a scripting language because there is usually no good reason to have HTML in the script when you separate the application logic from the template. The second style would be even better if short tags are available: <? ... ?> and <?= ... ?>. –  Lie Ryan Jan 10 '13 at 3:56
1  
@Piet: if you have troubles matching up braces, you probably never heard of indentation? Also, you should be able to configure to highlight the opening of endif in any decent editors. –  Lie Ryan Jan 10 '13 at 4:21
3  
@LieRyan Be nice. –  Rarst Jan 12 '13 at 14:09

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.

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.