Just a quick question to all you PHP gurus out there. I’m quite confident in programming PHP, over 3 years learning and recently I’ve noticed a new practise coming into play among PHP scripts on the web. No closing PHP tag. I was under the impression that a closing tag was both needed and required as standard practise. Now however, scripts leave it out. Could someone explain why and the benefits of doing this.
The reason why is because if any library file has any trailing whitespace after the closing tag, then that whitespace is treated as output for the browser. That unintended output will close your opportunity to set headers and possibly corrupt your output the moment you include that library. Since the closing tag is optional and there’s no practical benefit to include it in pure PHP files, you’re best to leave it off to prevent the possibility of introducing a hard to find bug.
That said. I consider it a bit messy. It’s like a html tag without a </html> tag. It’ll work just fine.
Omitting the tag does have advantages but the same can be achieved using output buffering, which is enabled by default in PHP anyway.
I include it, simply because it’s cleaner but it doesn’t really matter whether it’s there or not. The choices are either don’t put the tag or don’t put any whitespace after the tag. The former is obviously easier to see.
Well… half. Output buffering will allow you to set headers, but that whitespace is still part of the output, and that unintended output can corrupt your content. Real life example from just recently: XML is not valid if there is whitespace before the declaration, so when someone on my team left whitespace after a closing PHP tag, the RSS feeds stopped working. This kind of error is completely unnecessary and easily preventable. The only reasons to still use the closing tag exist only in our minds. Leaving it off violates our idealistic sense of strict syntax. But I think we need to fight past our mental obstacles and be pragmatic. If it helps, try thinking of the opening tag as a kind of shebang line.
The problem with this, however, is that you’re still altering, and possibly corrupting, the output. What if the output is supposed to have whitespace, such as an RFC document? Or what if the PHP is outputting binary data?
Omitting the closing tag prevents what would otherwise be a common mistake. And the kicker is that there’s no downside. There’s no practical reason not to omit the closing tag.
Like nearly everything in life there’s two side to this. I seemed to have opened a can of worms here. So just to clarify, I can do either but there are conditions or downsides to both? Why can’t there be a simple yes or no answer? Thanks for the answers guys but still a bit confused here.
There’s nothing ‘less clean’ about omitting the tag. It’s optional, and recommended you leave it out for files which are PHP only (or for the last block in a script if there is no HTML following it). I find it far cleaner to leave off given the risks of whitespace creeping in - it makes me nervous when I see them there, and I always remove them! Output buffering won’t help for the reasons stated above - neither will trimming output as the unintended whitespace could well appear in the middle of the final output anyway.
I always use the closing tag because I find it odd not to use one - it provides some visual clue that here the document ends.
Besides, it’s not entirely true about the white space. You can append one newline character after the closing tag and it will not be sent to the output. So if you want you can properly ‘end’ the last line with the closing tag - if you believe that each line should be terminated with a newline - for whatever esthetic reasons you may have :).
By the way, there has been some proposals recently in future versions of php to allow php-only without an opening php tag. This means that some day you can have pure php classes without having to add <?php to the top of file