No closing php tag

Hi guys,

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.

Cheers

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.

right ok, thanks for the explaination.

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.

There’s a good blog post discussing the merits of it here: http://choosetheforce.blogspot.co.uk/2008/05/should-you-close-that-php-tag.html

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.

Well you could argue that a simpler fix for that is:

ob_start(function($buffer) { return trim($buffer); } );

Because that fixes the issue at a much higher level, will work in every single use case and doesn’t enforce any coding standards as well as catching errors where someone’s done something like:


$str = <<<EOT
            <?xml version="1.0"?>

EOT;

My opinion is that if whitespace is a problem in the application, let the application handle it, don’t enforce arbitrary rules on the client code.

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 actually no downside to omitting the closing tag.

Also worth noting…

Here you go kids.

Read documentation from php website:
http://php.net/manual/en/language.basic-syntax.phptags.php

The clue you looking for is this (3rd paragraph):

If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file.

Ha ha, welcome to the forums, UncleD. Jeff Mott beat you to that link, though. (See the previous post.)

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

Yes, that would be better! How would they be recognised though? A different extension?

Not using the closing tag is just for avoiding blanks and other characters at the end of file.

With all due respect tutistech, lets try not to repeat what has already been said several times throughout a thread.