-2

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:

Community
  • 1
  • 1
padarom
  • 3,529
  • 5
  • 33
  • 57

1 Answers1

4

My opinion is that PHP being primarily a templating system, you shouldn't print HTML directly with it, only values. So I go for the second way.

I agree that writing <?php echo $variable; ?> is quitte messy (and ugly when there is many of them), but you can use the PHP short echo tags1, like this : <?= $variable ?>, which look way better and don't bloat your view. It is in fact exactly what templating systems built on PHP do, only without wrapping (and you get syntax highlighting as a bonus).

As a rule of thumb, avoid concatenation and evaluation of strings, that's a bad habit to take from a performance point of view, and not readible in the long term.

1 PHP short echo tags aren't discouraged anymore, because since PHP 5.4 they can't be turned off anymore, thus avoiding compatibility issues. On the other hand, PHP short open tag still are (unless I'm mistaken), so don't use them.

Elwinar
  • 9,103
  • 32
  • 40
  • I work a lot with PHP, and I find the lack of consistency (regarding short tags) disturbing :( – versvs Jun 03 '14 at 07:06
  • This lack of consistency come from the fact that prior to version 5.4, short tags were only an option in the config file. They couldn't be reliably used since you couldn't count on an host having them enabled. And in addition, the `` is an "invalid" opening tag in the XML point of view, being a preprocessor opening tag without indication about which program use to evaluate the instruction. – Elwinar Jun 03 '14 at 07:10
  • Thanks, short echo tags are really a way to consider. Always thought they were highly discouraged, but since one can't disable them anymore it might be the best way. – padarom Jun 03 '14 at 07:23
  • They were, but aren't anymore, thanks to PHP 5.4. PHP can be once again what it really is: a templating system. All those smarty & co written in PHP are just misunderstandings of what PHP is… – Elwinar Jun 03 '14 at 07:30