What the…?> PHP and unwanted whitespace

While working with some older PHP projects we’re migrating to our servers for some clients, I’ve run into a number of “unwanted whitespace” errors in PHP code. Unwanted whitespace can cause header output errors when they proceed a header call, or causing binary files loaded with readfile to break.

Typically, the cause of these errors has been the use of ?> followed by one or more whitespace characters. The extra whitespace was either added by the original author (and overlooked thanks to a low error_reporting level) or by a none-too-helpful FTP client.

After spending too long hunting down the whitespace (made necessary by the use of unnecessary code), I Twittered about the problem and a deluge of discussion followed.

Tradition and Convention vs. Need

According to the PHP manual on the topic of instruction separation, the use of ?> at the end of a file is completely optional. That said, the use of ?> has been standard fare for decades. At this point in history, most of us are so used to seing it dangling uselessly at the bottom of our code, that removing it feels painful or even dangerous.

Personally, I’d never thought to question it’s existence until I read the Doctrine ORM for PHP coding standards. Coding standards typically consist of a four spaces vs. tab debate, where one should put one’s curly braces, and class naming conventions. When I read Doctrine’s, I was surprised to find them start out with the requiring that ?> not be used at the end of files.

Since then, I’ve found other projects that require it be left off and, most recently, spent hours frustrated by the fact that someone before me didn’t leave it off.

Necessity, the Mother of Invention Convention

There seems to be growing movement among projects to drop the use of ?>. Most PHP coders, however, aren’t a part of those project. Many of us stumble across these things by accident, and overlook them as an oddity of a particular project (as I originally did with Doctrine) rather than a best practice for all PHP code. My hope is that my Twittering about it, this blog post, and subsequent debate/promotion/usage of the practice will save us all time down the road.

Discontinuing the use of ?> where it’s not needed is one way we can make the world a better place. No longer using PHP 3 and 4 and moving to PHP 5 would also help, but that’s another blog post.

15 Responses to “What the…?> PHP and unwanted whitespace”

  1. Patrick Says:

    Great article and interesting to read. While I’m not the main programmer in our company I’m glad you made me aware of this.

  2. Allain Lalonde Says:

    But we should preserve symmetry at all cost! Just kidding.

    Nice article. What are your thoughts on tabs vs spaces anyway?

  3. Colin Says:

    I’ve never seen this mentioned in any of the PHP books I’ve read/used, not in any online tutorials. It is an excellent bit of advice. Perhaps just a little bit more discussion on when its use is preferable or necessary. I assume just for embedded PHP in HTML pages, which I do very little of. Any other cases?

  4. Andrew Says:

    Thanks for this. As a self-taught PHP guy, I’m always looking for ways to standardize what we produce.

  5. Benjamin Young Says:

    @Allian for tabs vs. spaces I prefer tabs because it’s a single keystroke. That’s my only reason, and it’s not worth defending. Entirely personal preference.

    @Colin the ?> is only needed when PHP interpretation *needs* to stop–meaning, something other than PHP needs to be output next (HTML, JavaScript, XML, etc).

    @Andrew happy to help.

  6. Jon’s Blog » What the…?> PHP and unwanted whitespace Says:

    […] Check out the article and leave your feedback! […]

  7. Brade Says:

    Wow, this is news to me as well. Never knew the closing ?> was considered optional, or that it could be such a headache.

    However since most of us PHP developers use frameworks these days, the ball would seem to be mostly in their court on this matter. But I’ll do what I can, when I can.

  8. Benjamin Young Says:

    @Brade good point on frameworks. As mentioned above, Doctrine and Zend are doing it. Hopefully more will follow their lead.

  9. Code Conformity « Frontier Label Tech Blog Says:

    […] the PEAR coding standards seem to be what most people are relying on.  Even so, thanks to a post from Big Blue Hat, I can help prevent issues in the future with my PHP scripts.  I had no idea that one could […]

  10. Charissa Meems Says:

    Wow! Thanks for pointing that out!

    - C

  11. Carl Says:

    Add my thanks here too, much appreciated.

  12. coskunlar vinc Says:

    But we should preserve symmetry at all cost! Just kidding.

  13. Chris Says:

    Now I know there are lot of ppl out there who use Notepad, just because they can - personally I use an IDE and anytime I lose the last ?> the IDE goes all funny and gets the color coding confused; So personally I dont think the situation is so simple as clearly the IDE makers need to as follow this convention.

  14. Benjamin Young Says:

    @Chris, I guess it depends on what IDE your using. Eclipse does a fine job without the ending ?>, and this is the first I’ve heard of an editor tanking without the ?>

  15. potherca Says:

    Maybe a bit late in the game, but still…

    I’ve worked for/with a lot of different companies and cutting of the closing tag is one of the first things I tend to add to the coding standards (if they have any… otherwise I just enforce my own ;-)

    It hasn’t always gone down without a fight though.
    One of the main aids I offer the programmers who don’t feel comfortable, despite agreeing with the premise, is to end the file with ‘# ? >’ (can’t use ‘#?>’ as the closing tag would still be recognized as such by the parser) or ‘#EOF’

    Both ways you still have an end-
    of-file marker, without risking all of the hassle described above.

Leave a Reply

© 2007-2010 BigBlueHat
Close
E-mail It