php <? vs - Why would one omit the close tag?
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:
Is there another good reason to skip the ending php tag?
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.
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 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?
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.
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:
If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects because PHP will start output buffering when there is no intention from the programmer to send any output at that point in the script.
Well, I know the reason, but I can't show it:
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.
- Would be logical to close any opened tag, like with other languages. Not only X(HT)ML tags, but as well curly braces, brackets...
- Less confusing for beginners.
- Avoids headache with adding inadvertently whitespaces after the closing tag, because it breaks the header() function behavior... Some editors or FTP clients / servers are also known to change automatically the end of files (at least, it's their default configuration)
- PHP manual says closing tag is optional, and Zend even forbids it.
I would say that the arguments in favor of omitting the tag look stronger (helps to avoid big headache with header() + it's PHP/Zend "recommendation"). I admit that this isn't the most "beautiful" solution I've ever seen in terms of syntax consistency, but what could be better ?
As my question was marked as duplicate of this one, I think it's O.K. to post why NOT omitting closing tag
?> can be for some reasons desired.
- With complete Processing Instructions Syntax (
<?php ... ?>) PHP source is valid SGML document, which can be parsed and processed without problems with SGML parser. With additional restrictions it can be valid XML/XHTML as well.
Nothing prevents you from writing valid XML/HTML/SGML code. PHP documentation is aware of this. Excerpt:
Note: Also note that if you are embedding PHP within XML or XHTML you will need to use the < ?php ?> tags to remain compliant with standards.
Of course PHP syntax is not strict SGML/XML/HTML and you create a document, which is not SGML/XML/HTML, just like you can turn HTML into XHTML to be XML compliant or not.
At some point you may want to concatenate sources. This will be not as easy as simply doing
cat source1.php source2.phpif you have inconsistency introduced by omitting closing
?>it's harder to tell if document was left in PHP escape mode or PHP ignore mode (PI tag
<?phpmay have been opened or not). Life is easier if you consistently leave your documents in PHP ignore mode. It's just like work with well formatted HTML documents compared to documents with unclosed, badly nested tags etc.
It seems that some editors like Dreamweaver may have problems with PI left open .
There are 2 possible use of php code:
- PHP code such as class definition or function definition
- Use PHP as a template language (i.e. in views)
in case 1. the closing tag is totally unusefull, also I would like to see just 1 (one) php open tag and NO (zero) closing tag in such a case. This is a good practice as it make code clean and separate logic from presentation. For presentation case (2.) some found it is natural to close all tags (even the PHP-processed ones), that leads to confution, as the PHP has in fact 2 separate use case, that should not be mixed: logic/calculus and presentation