SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    no closing php tag

    Hi guys,

    Just a quick question to all you PHP gurus out there. I'm quite confident in programming PHP, over 3 years learning and recently I've noticed a new practise coming into play among PHP scripts on the web. No closing PHP tag. I was under the impression that a closing tag was both needed and required as standard practise. Now however, scripts leave it out. Could someone explain why and the benefits of doing this.

    Cheers

  2. #2
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,148
    Mentioned
    14 Post(s)
    Tagged
    0 Thread(s)
    The reason why is because if any library file has any trailing whitespace after the closing tag, then that whitespace is treated as output for the browser. That unintended output will close your opportunity to set headers and possibly corrupt your output the moment you include that library. Since the closing tag is optional and there's no practical benefit to include it in pure PHP files, you're best to leave it off to prevent the possibility of introducing a hard to find bug.
    "First make it work. Then make it better."

  3. #3
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    right ok, thanks for the explaination.

  4. #4
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    988
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    That said. I consider it a bit messy. It's like a html tag without a </html> tag. It'll work just fine.

    Omitting the tag does have advantages but the same can be achieved using output buffering, which is enabled by default in PHP anyway.

    I include it, simply because it's cleaner but it doesn't really matter whether it's there or not. The choices are either don't put the tag or don't put any whitespace after the tag. The former is obviously easier to see.

    There's a good blog post discussing the merits of it here: http://choosetheforce.blogspot.co.uk...t-php-tag.html

  5. #5
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,148
    Mentioned
    14 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TomB View Post
    Omitting the tag does have advantages but the same can be achieved using output buffering, which is enabled by default in PHP anyway.
    Well... half. Output buffering will allow you to set headers, but that whitespace is still part of the output, and that unintended output can corrupt your content. Real life example from just recently: XML is not valid if there is whitespace before the declaration, so when someone on my team left whitespace after a closing PHP tag, the RSS feeds stopped working. This kind of error is completely unnecessary and easily preventable. The only reasons to still use the closing tag exist only in our minds. Leaving it off violates our idealistic sense of strict syntax. But I think we need to fight past our mental obstacles and be pragmatic. If it helps, try thinking of the opening tag as a kind of shebang line.
    "First make it work. Then make it better."

  6. #6
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    988
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    Well you could argue that a simpler fix for that is:

    PHP Code:
    ob_start(function($buffer) { return trim($buffer); } ); 
    Because that fixes the issue at a much higher level, will work in every single use case and doesn't enforce any coding standards as well as catching errors where someone's done something like:

    PHP Code:
    $str = <<<EOT
                <?xml version="1.0"?>

    EOT;
    My opinion is that if whitespace is a problem in the application, let the application handle it, don't enforce arbitrary rules on the client code.

  7. #7
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,148
    Mentioned
    14 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TomB View Post
    Well you could argue that a simpler fix for that is:

    PHP Code:
    ob_start(function($buffer) { return trim($buffer); } ); 
    The problem with this, however, is that you're still altering, and possibly corrupting, the output. What if the output is supposed to have whitespace, such as an RFC document? Or what if the PHP is outputting binary data?

    Omitting the closing tag prevents what would otherwise be a common mistake. And the kicker is that there's no downside. There's no practical reason not to omit the closing tag.
    "First make it work. Then make it better."

  8. #8
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like nearly everything in life there's two side to this. I seemed to have opened a can of worms here. So just to clarify, I can do either but there are conditions or downsides to both? Why can't there be a simple yes or no answer? Thanks for the answers guys but still a bit confused here.

  9. #9
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,148
    Mentioned
    14 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by freakystreak View Post
    Like nearly everything in life there's two side to this. I seemed to have opened a can of worms here. So just to clarify, I can do either but there are conditions or downsides to both? Why can't there be a simple yes or no answer? Thanks for the answers guys but still a bit confused here.
    There's actually no downside to omitting the closing tag.

    Also worth noting...

    * Several major players in the PHP world were asked to vote on coding standards, and there was strong agreement to omit the closing tag. https://github.com/php-fig/fig-stand...style-guide.md

    * Omitting the closing tag has become such a widely accepted practice that it's even in the manual. "If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file." http://php.net/manual/en/language.ba...ax.phptags.php
    "First make it work. Then make it better."

  10. #10
    SitePoint Member
    Join Date
    Sep 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here you go kids.

    Read documentation from php website:
    http://php.net/manual/en/language.ba...ax.phptags.php

    The clue you looking for is this (3rd paragraph):
    If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file.

  11. #11
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    23,585
    Mentioned
    411 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by UncleD View Post
    Here you go kids.
    Ha ha, welcome to the forums, UncleD. Jeff Mott beat you to that link, though. (See the previous post.)

  12. #12
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    There's nothing 'less clean' about omitting the tag. It's optional, and recommended you leave it out for files which are PHP only (or for the last block in a script if there is no HTML following it). I find it far cleaner to leave off given the risks of whitespace creeping in - it makes me nervous when I see them there, and I always remove them! Output buffering won't help for the reasons stated above - neither will trimming output as the unintended whitespace could well appear in the middle of the final output anyway.

  13. #13
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    925
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    I always use the closing tag because I find it odd not to use one - it provides some visual clue that here the document ends.

    Besides, it's not entirely true about the white space. You can append one newline character after the closing tag and it will not be sent to the output. So if you want you can properly 'end' the last line with the closing tag - if you believe that each line should be terminated with a newline - for whatever esthetic reasons you may have .

  14. #14
    SitePoint Member
    Join Date
    Sep 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By the way, there has been some proposals recently in future versions of php to allow php-only without an opening php tag. This means that some day you can have pure php classes without having to add <?php to the top of file

  15. #15
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    988
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    Yes, that would be better! How would they be recognised though? A different extension?

  16. #16
    SitePoint Member
    Join Date
    Oct 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not using the closing tag is just for avoiding blanks and other characters at the end of file.

  17. #17
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by tutistech View Post
    Not using the closing tag is just for avoiding blanks and other characters at the end of file.
    With all due respect tutistech, lets try not to repeat what has already been said several times throughout a thread.
    ictus==""


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •