views:

1471

answers:

5

When mixing PHP and HTML, what is the proper indentation style to use? Do I indent so that the outputted HTML has correct indentation, or so that the PHP/HTML mix looks properly formatted (and is thus easier to read)?

For example, say I have a foreach loop outputting table rows. Which one below is correct?

PHP/HTML mix looks correct:

<table>
  <?php foreach ($rows as $row): ?>
    <tr>
      <?php if ($row->foo()): ?>
        <?php echo $row ?>
      <?php else: ?>
        Something else
      <?php endif ?>
    </tr>
  <?php endforeach ?>
</table>

Outputted HTML looks correct:

<table>
<?php foreach ($rows as $row): ?>
  <tr>
  <?php if ($row->foo()): ?>
    <?php echo $row ?>
  <?php else: ?>
    Something else
  <?php endif ?>
  </tr>
<?php endforeach ?>
</table>

I've found that when I run into this situation (quite frequently), I don't have a standard style to use. I know that there may not be a "correct" answer, but I'd love to hear thoughts from other developers.

+14  A: 

The PHP and the HTML should each be indented so that they are correct with respect to themselves in source form, irrespective of each other and of outputted form:

<table>
<?php foreach ($rows as $row): ?>
    <tr>
    <?php if ($row->foo()): ?>
        <?php echo $row ?>
    <?php else: ?>
        Something else
    <?php endif ?>
    </tr>
<?php endforeach ?>
</table>
chaos
+1  A: 

I generally put opening php tags at the beginning of the line, but indent whatever is inside the tags to match the html formatting. I don't do this, however, for simple echo statements since I use short-open tags. I think it makes simpler it when browsing through the file to find all the declarations.

    <table>
<?  foreach ($foo as $bar): ?>
      <tr>
<?    foreach ($bar as $baz): ?>

         <td><?=$baz?></td>

<?    endforeach ?>
      </tr>
<?  endforeach ?>
    </table>
tj111
+1: This is how I actually write (well, except I use braces, not colon/end form, and four spaces per indent level); I just adapted OP's conventions for convenience.
chaos
I got out of this habit as it makes it a lot harder to quickly scan through your page.
deceze
You should not use the short opening tags. You can run into various problems with your code connected to the php setting short_open_tag.
tharkun
+2  A: 
  1. Direct answer to your question: If you need to read the HTML output often, it might be a good thing to output well indented HTML. But the more common case will be that you need to read your php source code, so it is more important that the source is easily readable.
  2. Alternative to the two options you mentioned: See chaos' or tj111's answer.
  3. Better still in my opinion: Don't mix HTML and php, use a template engine instead.
Treb
Thanks for the comments Treb. Even if I used a templating engine, the question would still exist: how to properly indent HTML/templating engine tags.
James Skidmore
PHP is a templating engine. :)
chaos
@chaos: I knew that answer would come... Maybe it is, but what I see in the example codes here is not a proper use of a template engine, but a bad mixture of code and design.
Treb
+2  A: 

You can always use a bit of whitespace too to help readability. Building on chaos' indentation:

<table>

<?php foreach ($rows as $row): ?>

    <tr>

    <?php if ($row->foo()): ?>
        <?php echo $row ?>
    <?php else: ?>

        Something else

    <?php endif ?>

    </tr>

    <?php endforeach ?>

</table

The only downside with this is if you have a lot of mixed code it can make your document twice as long, which makes for more scrolling. Although if you have this much mixed code you may want to consider a templating engine.

Peter Spain
You just added some line breaks....that don't really add to the clarity at all IMHO. Get a decent IDE and it should color it nicely for you.
Mark
This really is an entirely subjective matter, so of course what may be an aide for some wont do a thing for others. :)
Peter Spain
+2  A: 

I often pondered this question too, but then I realized, who gives a damn what the HTML looks like? Your users shouldn't be looking at your HTML anyway. It's for YOU to read, and maybe a couple other developers. Keep the source code as clean as possible and forget about what the output looks like.

That said, I prefer your first example, although personally I wouldn't use so many opening and closing PHP tags (I prefer HEREDOCs or echos in most cases).

Mark