I keep reading it is poor practice to use the PHP close tag ?>
at the end of the file. The header problem seems irrelevant in the following context (and this is the only good argument so far):
Modern versions of PHP set the output_buffering flag in php.ini If output buffering is enabled, you can set HTTP headers and cookies after outputting HTML because the returned code is not sent to the browser immediately.
Every good practice book and wiki starts with this 'rule' but nobody offers good reasons. Is there another good reason to skip the ending PHP tag?
Well, I know the reason, but I can't show it:
Source: http://framework.zend.com/manual/en/coding-standard.php-file-formatting.html
In addition to everything that's been said already, I'm going to throw in another reason that was a huge pain for us to debug.
Apache 2.4.6 with PHP 5.4 actually segmentation faults on our production machines when there's empty space behind the closing
php
tag. I just wasted hours until I finally narrowed down the bug with strace.Here is the error that Apache throws:
It isn't a tag…
But if you have it, you risk having white space after it.
If you then use it as an include at the top of a document, you could end up inserting white space (i.e. content) before you attempt to send HTTP headers … which isn't allowed.
The reason you should leave off the php closing tag (
?>
) is so that the programmer doesn't accidentally send extra newline chars.The reason you shouldn't leave off the php closing tag is because it causes an imbalance in the php tags and any programmer with half a mind can remember to not add extra white-space.
So for your question:
No, there isn't another good reason to skip the ending php tags.
I will finish with some arguments for not bothering with the closing tag:
People are always able to make mistakes, no matter how smart they are. Adhering to a practice that reduces the number of possible mistakes is (IMHO) a good idea.
PHP is not XML. PHP doesn't need to adhere to XMLs strict standards to be well written and functional. If a missing closing tag annoys you, you're allowed to use a closing tag, it's not a set-in-stone rule one way or the other.
If I understand the question correctly, it has to do with output buffering and the affect this might have on closing/ending tags. I am not sure that is an entirely valid question. The problem is that the output buffer does not mean all content is held in memory before sending it out to the client. It means some of the content is.
The programmer can purposely flush the buffer, or the output buffer so does the output buffer option in PHP really change how the closing tag affects coding? I would argue that it does not.
And maybe that is why most of the answers went back to personal style and syntax.
According to the docs, it's preferable to omit the closing tag if it's at the end of the file for the following reason:
PHP Manual > Language Reference > Basic syntax > PHP tags