prettier / plugin-php

Prettier PHP Plugin
https://loilo.github.io/prettier-php-playground/
MIT License
1.75k stars 130 forks source link

semicolons in short echo tags #609

Closed joemaller closed 5 years ago

joemaller commented 6 years ago

Has there been any discussion about semicolons in short echo tags?

Using dev builds, this:

<?= $content ?>

Changes to this (added semicolon):

<?= $content; ?>

Nearly every PHP short echo tag example I found omits semicolons. Because short echo tags are usually found in template contexts, the less-verbose the better.

There don't appear to be any PSR recommendations about this, though the CakePHP project (the only big-project guidance I could find) recommends omitting short echo tag semicolons:

// wrong = semicolon, no spaces
<td><?=$name;?></td>

// good = spaces, no semicolon
<td><?= $name ?></td>

If short echo tag semicolons are the way Prettier-PHP is going, I'd like to start moving my code in that direction. If short echo tag semicolons are under discussion or being considered as an option, I vote for omitting them.

alexander-akait commented 6 years ago

@joemaller Hm, interesting, php require ; in all places, except you case, we have standard --no-semi option in prettier and we can implement behavior without semi using this option

alexander-akait commented 6 years ago

It should be very easy, but let's wait what says other

czosel commented 6 years ago

I don't recommend using an option for this, because we agreed that options are only for very rare cases where

  1. there is official standard recommendation
  2. there is no unofficial "community standard" and both variants are popular
  3. not offering an option would hinder adoption

While (1) seems to be the case here, (2) and (3) probably aren't. My feeling is also that the "community standard" is having no semicolons in short echo tags - if anyone has a different feeling we could collect some data of popular open source projects.

joemaller commented 6 years ago

...quieter than I'd hoped.

I think reasonable next step would be collecting examples of short-echo tag usage, to establish that there is in fact a community standard, even though that usage pattern is not formally declared and hasn't yet been recognized in a PSR.

joemaller commented 6 years ago

Semicolons in PHP Short Echo Tags

Despite the short-echo syntax having been supported by default since PHP 5.4 was released back in 2012 (EOL 2015), it's difficult to find clear guidance about best practices. Most often, information is confounded with older advice about avoiding short-open tags (<? ?>).

Examples are difficult to find since random snippets of non-alphanumeric syntax like <?= are generally not considered valid search terms.

PHP Documentation

The Official PHP documentation omits semicolons from short-echo examples:

example #2:

  1. You can use the short echo tag to <?= 'print this string' ?>.
    It's always enabled in PHP 5.4.0 and later, and is equivalent to
    <?php echo 'print this string' ?>.

More from the PHP docs on implied semicolons:

The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block.

Stack Overflow

What does '<?=' mean in PHP?

Note that the ; is redundant; as the answers suggest this short-tag expands to an echo with a semicolon added to the end, as per the php documents. @not-just-yeti

CakePHP

Short echo is recommended in CakePHP template contexts, no semicolons:

// wrong = semicolon, no spaces
<td><?=$name;?></td>

// good = spaces, no semicolon
<td><?= $name ?></td>

WordPress

WordPress still supports PHP versions back to 5.2.4, so they discourage short-echo tags. However, their coding standards example omits semicolons: <?= $var ?>

Miscellaneous examples

czosel commented 6 years ago

:100: I'd say that's enough evidence to justify omitting semicolons in our output.

QWp6t commented 6 years ago

Agreed. Even though I always include them in my own code, I think it's clear that omitting them seems to be much more common.

alexander-akait commented 5 years ago

We avoid ; for declare with html so let's avoid ; in short php tags