Trailing Whitespace and PSR-12 Formatting in Mixed PHP/HTML Files

462 Views Asked by At

I'm trying to be more strict about my adherence to PSR-12 and I'm not sure how to handle a specific situation involving whitespace.

In the past, I have often used trailing whitespace after a PHP tag to ensure that there is space in the resulting HTML. For example:

<?= $address_line_1 ?><br>
<?= $address_line_2 ?><br>
<?= $city ?>,
<?= $state ?> 
<?= $postal_code ?>

Note the space after <?= $state ?>. Without this trailing whitespace, my code prints "NY63764" instead of "NY 63764". (Obviously, in this example, I would put the state and postal code on the same line, but there are frequent cases where doing so would push me past the 120 character character soft limit).

If the trailing space is incorrect, what is the correct strategy? The options I can think of include:

  • Exceed the 120 character line limit
  • Print a space (e.g. <?= $state . ' ' ?>)
  • Use a non-breaking space (i.e. <?= $state ?>&nbsp;)

Those seem like poor options. What am I missing?

0

There are 0 best solutions below