Omit the PHP Closing Tag
Published: January 27th, 2009 by: Andrew
This is just a simple tip that presents a simple proposition - stop closing your PHP tags. Why? I will explain in this article. Keep in mind that this idea / standard only applies to PHP files that are only for PHP - no HTML.
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.
Solution: If that last closing tab is removed, PHP will not output that whitespace. What I have started to do is comment the closing tag out. This will make PHP process the whitespace instead of sending it to the browser, and since the tag is still there, you know it’s the end of the file in case it gets truncated for whatever reason.
If this sounds like an unusual solution, the Zend Framework makes this a requirement in their reference guide. Although they lost a bit of credit with me when I saw they require spaces over tabs. 🙂
aftersyok
Nov 5th, 2010
9:08 pm
i was wondering why some new programmers in my company do not use php closing tag. these guys are better php programmer than me eventhough i’m 5 years older.
however, while i have been coding PHP almost as long as you are, somehow I have never encounter even once the unintentional space-after-php-closing-tag output problem. guess i’ve been so lucky through these years that i never noticed such problem. or it could have been something with me using the @ob_start() function in each starting php files i use.
anyway thanks for the great, simple, and helpful tips.