[php] 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?
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
Well, there are two ways of looking at it.
- PHP code is nothing more than a set of XML processing instructions, and therefore any file with a
.phpextension is nothing more than an XML file that just so happens to be parsed for PHP code.
- PHP just so happens to share the XML processing instruction format for its open and close tags. Based on that, files with
.phpextensions MAY be valid XML files, but they don't need to be.
If you believe the first route, then all PHP files require closing end tags. To omit them will create an invalid XML file. Then again, without having an opening
<?xml version="1.0" charset="latin-1" ?> declaration, you won't have a valid XML file anyway... So it's not a major issue...
If you believe the second route, that opens the door for two types of
- Files that contain only code (library files for example)
- Files that contain native XML and also code (template files for example)
Based on that, code-only files are OK to end without a closing
?> tag. But the XML-code files are not OK to end without a closing
?> since it would invalidate the XML.
But I know what you're thinking. You're thinking what does it matter, you're never going to render a PHP file directly, so who cares if it's valid XML. Well, it does matter if you're designing a template. If it's valid XML/HTML, a normal browser will simply not display the PHP code (it's treated like a comment). So you can mock out the template without needing to run the PHP code within...
I'm not saying this is important. It's just a view that I don't see expressed too often, so what better place to share it...
Personally, I do not close tags in library files, but do in template files... I think it's a personal preference (and coding guideline) based more than anything hard...
- 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 ?
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.
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.