PSR standard for short-echo tags spacing

1.3k Views Asked by At

The PSR coding standards are very clear about most of their conventions, including the very first entry in PSR-1:

PHP code MUST use the long <?php ?> tags or the short-echo <?= ?> tags; it MUST NOT use the other tag variations.

but what is not clear and has been questioned recently in our team is about the spacing, particularly for the short-echo tags. We would normally see a <?php opening tag on its own line at the top of a file or if it is an in-line statement then it will fail to parse if there is no space. So the rule here seems self explanatory.

<?php
//tag followed by new line - fine
$obj = new Foo();
?>
<p>Hello <?php echo $obj->printBar(); ?> World</p>
//tag followed by space - fine
<p>Hello <?phpecho $obj->printBar(); ?> World</p>
//tag no space - Parse Error

My question is what about the short-echo tags <?= ?>, with and with out a space after the opening tag parses fine - but which is PSR standard compliant?

<p>Hello <?= $obj->printBar(); ?> World</p>
//tag followed by space - parses okay
<p>Hello <?=$obj->printBar();?> World</p>
//tag no space - parses okay
1

There are 1 best solutions below

0
On

Add a space for consistency across the short and long PHP open tags.