Omit the closing php tag

I found this little tidbit in the Zend Framework Programmer’s Reference Guide (in the Coding Standard appendix):

For files that contain only PHP code, the closing tag (“?>”) is never permitted. It is not required by PHP. Not including it prevents trailing whitespace from being accidentally injected into the output.

To be honest, I didn’t realize that the closing tag was optional in a file of pure PHP. Somehow it doesn’t feel “right” to leave it off — something about the lack of symmetry that coding ingrains in us software developers. I see what they’re trying to accomplish, but surely there’s got to be a less hacky way to go about it?

Maybe the PHP developers should consider an alternate open tag, one that doesn’t imply that there will be a matching close tag. Something like


At any rate, this is something to consider, especially if your PHP code needs to output something other than garden-variety HTML. If you’re kicking out binary data like a GIF or something along those lines, you don’t want a stray newline to be output with your binary.


Leave a comment

Your email address will not be published. Required fields are marked *