PHP in HTML or HTML in PHP?

What’s nice about PHP (among other things) is that code blocks can easily be nested inside the HTML of a page. This way you in insert dynamic content just like that. Of course you can also go the other way and echo out HTML via PHP.

My simple rule is: When you have a clear overweight of one or the other use this as your main code and nest-in the other.

This makes source code way more readable than, for example, when you have orgies of opening and closing PHP tags inside “some” HTML tags!

Let me give you an example (do you recognize where it’s from?).

Original Code

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div>
<?php the_content('Read the rest of this entry &raquo;'); ?>
</div>
<?php endwhile; ?>
<?php else : ?>
 <b>Not Found</b>
<?php _e("Sorry, but you are looking for something that isn't here."); ?>
<?php endif; ?>

Cleaned up code

<?php
if( have_posts() ) {

  while( have_posts() ) {
    
    the_post();

    echo "<div>\n";

    the_content( 'Read the rest of this entry &raquo;' );

    echo "</div>\n";

  }
}
else {
  
  echo "<b>Not Found</b>\n";
  echo __( 'Sorry, but you are looking for something that isn't here.' );

}
?>

Well, I’ve also done some more formatting (according to my coding standard) and replaced the (in my mind) horrible and old-school : ENDIF ENDWHILE grammar. But you get my point, right?

When you’re writing new code, always keep that in mind. And if you’re refactoring old (legacy) code this cleaning up should be your absolute first step!

About these ads

2 thoughts on “PHP in HTML or HTML in PHP?

  1. I feel most people would agree that the best answer is, do whatever keeps them separate. Embedding HTML in PHP causes syntax highlighting headaches and loses readability. You also have to be wary of the PHP parser when writing/editing the HTML i.e. escaping string or variable delimiters, etc.

    To modify your WordPress(?) example (forgive unpredictable anti-formatting):

    Not Found

    1. Good point and thanks for the feedback. A clear separation would of course be ideal. Though it’s getting a bit religious there like when it comes to the proper use of templating engines.

      Personally, I agree you should separate your business logic and only feed the resulting data to the template engines or views. But to me it’s okay then to use PHP to build the HTML.
      And to avoid the pitfalls in terms of string escaping, the heredoc syntax can be useful.

      (I guess you wrote some code at the end of your comment. If so, it seems that WP has eaten it. Sry ’bout that, I’m looking into it).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s