Clarification required for PSR-2: The closing ?> tag MUST be omitted from files containing only PHP

456 views
Skip to first unread message

Greg Sherwood

unread,
Jan 19, 2015, 11:12:09 PM1/19/15
to php-f...@googlegroups.com
I've had someone ask a question [1] about this part of PSR-2. Specifically, if you have a file with both PHP and inline HTML, do you need to omit the closing PHP tag if the last part of the file is PHP code?

A really simple example:
1: <?php
2: echo 'foo';
3: ?>
4: </body>
5: </html>

Obviously the closing tag stays. But what if it is the other way around:
1: <html>
2: <head></head>
3: <body>
4: <?php
5: echo 'foo';
6: ?>

Can you leave the closing tag in the file or do you need to omit it here?

PHP_CodeSniffer currently enforces that the closing tag must be omitted if it is the last content in the file. Do I need to change PHP_CodeSniffer so that any non-empty inline HTML tokens allow the closing tag to remain? If so, is the closing tag mandatory in such a case or is it just not checked at all?

Thanks

Greg

Pádraic Brady

unread,
Jan 20, 2015, 3:04:54 PM1/20/15
to Greg Sherwood, php-f...@googlegroups.com
Most consistent answer is to omit it IMO

Paddy
> --
> You received this message because you are subscribed to the Google Groups
> "PHP Framework Interoperability Group - Coding Style" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to php-fig-cs+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--

--
Pádraic Brady

http://blog.astrumfutura.com
http://www.survivethedeepend.com
Zend Framework Community Review Team
Zend Framework PHP-FIG Representative

Paul M. Jones

unread,
Jan 23, 2015, 9:40:28 AM1/23/15
to Greg Sherwood, php-f...@googlegroups.com

> On Jan 19, 2015, at 22:12, Greg Sherwood <gshe...@gmail.com> wrote:
>
> I've had someone ask a question [1] about this part of PSR-2. Specifically, if you have a file with both PHP and inline HTML, do you need to omit the closing PHP tag if the last part of the file is PHP code?
>
> A really simple example:
> 1: <?php
> 2: echo 'foo';
> 3: ?>
> 4: </body>
> 5: </html>
>
> Obviously the closing tag stays. But what if it is the other way around:
> 1: <html>
> 2: <head></head>
> 3: <body>
> 4: <?php
> 5: echo 'foo';
> 6: ?>
>
> Can you leave the closing tag in the file or do you need to omit it here?

PSR-2 is effectively silent on the matter. The rule in question states:

"The closing ?> tag MUST be omitted from files containing only PHP."

If the file contains anything other than PHP, then the rule does not apply.

I suggest that a PSR-2-specific sniffer likewise be silent in that situation as well (i.e., in a file that does not contain only PHP). Probably easier said than done.


--
Paul M. Jones
pmjo...@gmail.com
http://paul-m-jones.com

Modernizing Legacy Applications in PHP
http://mlaphp.com



Chuck Burgess

unread,
Jan 23, 2015, 9:45:24 AM1/23/15
to Paul M. Jones, Greg Sherwood, php-f...@googlegroups.com
Hmm...  if there is even just *one* closing tag somewhere that's *not* the last non-whitespace characters, then can one assume that there is non-PHP in the file?  I'm thinking that alone might be enough to denote a parsed file as "containing non-PHP".

Pádraic Brady

unread,
Jan 23, 2015, 11:19:18 AM1/23/15
to Demon...@gmail.com, Paul M. Jones, Greg Sherwood, php-f...@googlegroups.com
In thinking about it some more, I believe the appropriate approach is
to effectively remain silent and let the user use whichever they
prefer. I'm thinking about likely formatting:

<html>
<head>
...
<?php echo $foo ?>
<?php echo $bar ?>
<?php echo $baz ?>
Intermediate content...
<?php echo $foo ?>


I can see how enforcing the removal of that closing tag in the last
template position could become irritating. In a one-liner style it may
be inconsistent to try applying a PSR intended for PHP code to
template markup. In the above, it's not simply enough to remove the
closing tag, you then also need to add the implicit semi-colon to
terminate the final statement.

In any case, as Paul indicated, since we are indeed silent on this (we
explicitly state that we're silent even!), it has no place is a PSR2
ruleset.

Paddy

Chuck Burgess

unread,
Jan 23, 2015, 12:07:12 PM1/23/15
to Paul M. Jones, Greg Sherwood, php-f...@googlegroups.com
Oh, I agree on PSR2 being silent on it.  I was mainly thinking about Paul's observation of it being "easier said than done" in the sniffer.  Since the rule itself states "files containing only PHP", then I thought perhaps that check for other closing tags earlier than the end of the file would be an easy way to decide "file contains only PHP" or "not", thus toggling whether or not the PSR2 rule for "no closing tag at end" would be considered at runtime.

Greg Sherwood

unread,
Jan 26, 2015, 4:11:31 PM1/26/15
to php-f...@googlegroups.com
Thanks everyone for taking the time to reply. I'll change PHPCS to ignore this check if it find any non-empty HTML tokens in the file.
Reply all
Reply to author
Forward
0 new messages