When creating output almost entirely by PHP, I wonder how to indent my code to be readable.
Currently I do something like this:
echo '<div>';
echo '<header'>;
echo "<span>$element</span>";
echo '</header>';
echo '<footer>';
echo "<span>$anotherElement</span>";
echo '</footer>';
echo '</div'>;
This does work, but in my opinion it gets way too messy pretty quick when adding more nested elements to it. Is there a better way to do it? Closing the PHP tag and adding the HTML as in the next example e.g?
<div>
<header>
<span><?php echo $element; ?></span>
</header>
<footer>
<span><?php echo $anotherElement; ?></span>
</footer>
</div>
Of those two examples, what is to prefer, considering the readability of the code?
Also I don't usually just do one variable assignment, but instead echo lots of variables, which is why I don't use the second version too often. Having 10 <?php echo '...'; ?>
in 10 rows also looks kind of awkward to me.
Which experiences have you made? Which methods established to be readable and maintainable even at a larger scale?
There are other questions on this topic, though they are unfortunately not completely the same:
- PHP - indent block of html (I don't really care about the indentation of the resulting HTML markup)
- How to properly indent PHP/HTML mixed code? (He does the second version, but is it the best?)