Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

semicolons in short echo tags #609

Closed
joemaller opened this issue Sep 11, 2018 · 8 comments · Fixed by #886
Closed

semicolons in short echo tags #609

joemaller opened this issue Sep 11, 2018 · 8 comments · Fixed by #886

Comments

@joemaller
Copy link

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
Copy link
Member

@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
Copy link
Member

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

@czosel
Copy link
Collaborator

czosel commented Sep 11, 2018

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
Copy link
Author

...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
Copy link
Author

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
Copy link
Collaborator

czosel commented Sep 24, 2018

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

@QWp6t
Copy link

QWp6t commented Oct 4, 2018

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
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants