Should I Omit (?>) PHP Closing Tag?

Friday, November 19, 2010
I never thought that the PHP closing tag was optional after years of working with PHP.  Before,  I consider it as a standard to have a closing tag just like the conditionals (if-else) and the loops (for, do-while, etc.) . . . you name it. Closure is enforced. Failing to do so results in a compile-time error.

A year ago, I became part of a project with a senior developer and he gave us a copy of coding standard that he wants.  The guideline was pretty impressive and I realized a lot of things which I can improved when it comes to coding.  Seeing that guideline convinced me that this man is really good. Then I started browsing his codes, then I get to notice that he isn't using the PHP closing tag on the controller and libraries we are using.  We are using Code Igniter. Boggled with this, I asked our boss why is he not using the closing tag at all. That was the time I realized that it is optional when the file is all PHP code and more importantly it prevents problems with the trailing white space.

PHP developers frequently encounter the problem with sending headers and cookies after output has been sent to the browser, but that problem can also happen with unintentional output. If whitespace gets inserted after the end of a PHP code block, that can produce unintentional output when that PHP script is included.

Zend Framework says that "For files that contain only PHP code, the closing tag ("?>") is never permitted. It is not required by PHP, and omitting it´ prevents the accidental injection of trailing white space into the response." Keep in mind that this idea / standard only applies to PHP files that are only for PHP - no HTML.

Kindly Bookmark and Share it: