SitePoint Sponsor

User Tag List

View Poll Results: Removing the ?> at the end of PHP is..

Voters
23. You may not vote on this poll
  • Bad Practice

    1 4.35%
  • Good Practice

    11 47.83%
  • Neutral, does not matter to me.

    11 47.83%
Page 1 of 2 12 LastLast
Results 1 to 25 of 26
  1. #1
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Leaving last line open without ?>

    I read an article on digg I think, and I don't have the link now, but it suggested leaving out the ?> at the last line of your php file (of entire site) so that you won't have any white-space error for headers. I think he suggested it to be good practice and it defers the newbies, I guess I'm a newbie lol, I have never seen this done. He said it is not required but is it still bad?

    Would you consider this bad practice?

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Not really bad practice as its not a requirement however I am not sure of the reasoning with the headers/white-space error.

    You can add it or not - your choice. Personally I do close my blocks just as in javascript I always end my lines with a ;
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I think the message is to be totally aware that there are no hidden spaces after your closing ?> something which I somewhat maddeningly repeated do.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Some open source libraries have set a coding standard of always omitting the redundant closing tag at the very end of files so as to make it obvious to everyone that there is no trailing whitespace to cause any issues. Omitting the tag in that situation will make maintnance easier since it totally eliminates one possible cause of errors.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Omitting the tag makes complete sense to me. It serves no purpose.

    Zend Framework does this.
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by bhanson View Post
    Omitting the tag makes complete sense to me. It serves no purpose.

    Zend Framework does this.
    It is actually a part of the Zend framework coding standards that the tag MUST be omitted.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    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)
    It doesn't make a difference to me, but personally I always keep it in. I've never really had any trouble making sure there is no white space after it (and in fact, have never had any problems caused by it), it isn't difficult to check

  8. #8
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am used to omitting the closing tag from some PHP frameworks I have tried. Most of them recommend to omit it.

  9. #9
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Im kind of suprised that this is a good thing, but it does make sense. It's a strange thing I would have only heard from someone else to actually start doing.

    I don't think you'd find this in a book anywhere, or has anyone?

  10. #10
    SitePoint Addict silentcollision's Avatar
    Join Date
    Jun 2006
    Location
    New Zealand
    Posts
    388
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Article: http://www.yongfook.com/items/view/8...lopment-tricks

    6) Leave off the last ?> in php-only files

    Shock horror, you donít need to close your php tags in a php-only file. The reason you might not want to is because:

    * It makes you look cool and dangerous to people who donít know this tip. Kind of like the geek equivalent of smoking in front of your 10-year old cousin.
    * It prevents accidental whitespace from being output by your php file
    * If you add up the time you save by not typing those two characters in each file of every project you work on from now until you are dead, youíll find that youíll literally save valuable minutes of your life.

  11. #11
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I leave it off, because I used to add it, and I like to mix things up. Living on the edge.
    Doesn't hurt to have a procedure which will protect against white space issues from PHP only files.
    mikehealy.com.au
    diigital.com art, design . Latest Work ó Saturday Morning

  12. #12
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't say that it is bad practice but I have NEVER worked omitting closing tags yet in my experience and I don't think we have to follow the standard only because of not having the header sent error.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  13. #13
    SitePoint Guru
    Join Date
    Jul 2005
    Location
    Orlando
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can see the benefit of it, but I don't.

    I use a whitespace addon that trims whitespace that isn't needet every time I save.

  14. #14
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by rajug View Post
    I don't say that it is bad practice but I have NEVER worked omitting closing tags yet in my experience and I don't think we have to follow the standard only because of not having the header sent error.
    It isn't a general standard. Whether you include the closing ?> or not at the end of the file is up to you unless you are working on a project which dictates the standard one way or the other.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  15. #15
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mm I don't see how you can accidentally leave a whitespace in a file. I never have but that my just be because of the way that a code. I tend to open and close of a code block before defining it further so I rarely run into such problems.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  16. #16
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A drawback to doing this is if you one day want to merge the source of all or some files(maybe for performance or distribution reasons), you will need additional logic instead of just using a quick shell command which appends the contents of one file to another.

    However, I usually omit the end tag.

  17. #17
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I personally find it a little 'hacky' to omit the tag, after all, the closing tag is there to close is it not?

    I do find it strange though, to compensate for poor coding (erroneous white space), you purposely implement yet more poor coding.

    The mind boggles.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  18. #18
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by imaginethis View Post
    mm I don't see how you can accidentally leave a whitespace in a file. I never have but that my just be because of the way that a code. I tend to open and close of a code block before defining it further so I rarely run into such problems.
    You just need to accidentally touch the spacebar or enter key and if your editor doesn't show whitespace you will not know it is there until it breaks something.

    Really easy to position the cursor using the mouse and not tralise that there is a space or linefeed following it as well.

    When you do get such an error it can take ages to find the cause since you can't see the character causing the error.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  19. #19
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    I personally find it a little 'hacky' to omit the tag, after all, the closing tag is there to close is it not?
    I used to feel that way but the end of the file will also act as the close. Similar to leaving out mysql_close()

    Consider this. The last line of the file is HTML. Do you feel the need to have
    <?php
    ?>
    to "close" the php file?
    - Robert

  20. #20
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Seems weird to leave it off at first, but why not avoid extra whitespace where possible, eh? Sounds logical to me.
    Last edited by MarkDRussell; Dec 2, 2008 at 03:24.

  21. #21
    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)
    My editor strips all whitespace from the end of a line on save, and puts line numbers next to every line in the file. It is very easy for me to see any extra whitespace I might have, and remove it.

  22. #22
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Stormrider View Post
    My editor strips all whitespace from the end of a line on save, and puts line numbers next to every line in the file. It is very easy for me to see any extra whitespace I might have, and remove it.
    Some editors do that and some don't. When you have big projects such as Zend where lots of different people are working on it some will be using editors that don't and if they added the ?> and left any whitespace after it then that could potentially stop the script working. Since finding which of their tens of thousands of files has the unwanted blank would take a significant amount of time they set a standard of never including ?> at the end of any file.

    If it is a small project group with everyone using a browser that strips the trailing whitespace then there is no problem setting the standard to say that everyone working on that project must include it.

    Different standards for different situations. If you ever work on Zend you will be required to omit the ?> because it is forbidden to include it in their files.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  23. #23
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    It isn't a general standard. Whether you include the closing ?> or not at the end of the file is up to you unless you are working on a project which dictates the standard one way or the other.
    I am talking about this particular standard not other. So until I go with some frameworks that strictly follow this (particular this) standard, I don't think this is such important standard for any security issues..
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  24. #24
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In files that contain only PHP code, never include the closing tag. It can lead to whitespace being unexpectedly injected into the output and cause headers to be sent prematurely.
    Source: http://framework.maintainable.com/st..._standards.php

    This seems to be the explanation of most PHP frameworks why they don't include closing tags.

  25. #25
    SitePoint Evangelist
    Join Date
    May 2006
    Location
    Denmark
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wouldn't exactly call it bad practice to include the closing tag, so I voted neutral. I do not include it myself though as I usually code using ZF which requires that it isn't there. Since I started using ZF I've always done that for the reasons stated.


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
  •