Reputation: 2970
The Zend Framework coding standard mentions the following:
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 whitespace into the response.
However I do remember hearing about an issue (with tooling or including maybe?) where files needed to have closing tag.
Does anyone know of any issues (other than the developer issue of wanting symmetry) where you would need to have closing tags or are they generally a bad idea?
Upvotes: 26
Views: 3627
Reputation: 100160
If you use closing PHP tags then you can easily find files that have been accidentally truncated (e.g. haven't been uploaded completely).
However such problem should be fixed by making filesystem and transfers reliable rather than adding PHP "canary" tags :)
Another possible reason is that PEAR coding standards require it. They require it, because they require it and you're not allowed to question PEAR coding standards.
Upvotes: 11
Reputation: 29907
Checking for coding errors doesn't work when concating multiple files. I use the following shell command to check for errors.
find . -name '*.php' | xargs cat | php -l
Of course the following command still works (and displays the filename of the corrupted file)
find . -name '*.php' | while read filename; do php -l $filename | grep -v 'No syntax errors '; done
But this one is much much slower.
Upvotes: 1
Reputation: 33453
I always put them in because I view them as a fancy closing brace - as when I write conditionals and loops I put in the open/close brace pair before writing code, I do with PHP files.
Whether it's "right" or not, I semi-frequently find myself turning "pure" PHP files into "nonpure", and the closing tag is extremely helpful.
Upvotes: 2
Reputation: 3747
I always write the closing tag for php files, and purposefully don't follow it with spaces or eol.
The standard implies that if the closing tag is omitted, one will be implicitly supplied. But that doesn't exonerate the developer from writing proper html/xml.
And there may be cases where two files are combined outside of html/php processing, and a missing tag would cause extreme grief.
Upvotes: 3
Reputation: 45575
We use Drupal, which always omits the closing tag, and never had any problems because of it.
Since it always worked for Drupal (and I knew from reading the manual it was optional), I once created a configuration file (which contained only PHP code) without the closing tag. For some reason, it did not work until (at the insistence of a coworker) I added the closing tag. We didn't try to find out the cause at the time.
I have always used the closing tag in code I write; it looks more simmetrical, conceptually "closes" the opening tag, and I am always careful to not leave any extra whitespace after the closing tag.
Upvotes: 1
Reputation:
You could consider the php opening tag for php-only files, as a sort of shebang line (like #!/bin/bash), in that way, you don't need a closing tag.
We follow the ZF standard as well, no closing tag for php-only files. But I've never heard of any tool which couldn't parse a file because of a missing closing tag, and I haven't gotten into any problems as of yet for not using it.
Upvotes: 4
Reputation: 41857
i always use the closing tags, it just doesn't look nice without them. It's also not a valid xml Processing instruction without them (not that you care if the only thing in the file is php)
Upvotes: 1
Reputation: 1790
In reply to @gabriel1836, the closing tags on the server side are irrelevant when it comes to Flash applications; what's relevant is the payload returned by the server in response to a remoting request by the Flash application. Omitting the closing tag on PHP-only files will affect this only in ensuring that spurious output is not returned prior to headers being sent to your Flash app.
In reply to @CesarB, unless you can give a concrete, reproducible example, all you're doing is spreading FUD. The PHP manual is very clear that the closing tag is optional.
Upvotes: 0
Reputation: 276
Removing the closing tags when you can is probably a best practice really. The reason for that is because if you have any character (even whitespace) outside of the ?> in a php file it can stop thing such as headers to stop working if the character gets sent to the browser before the header gets set.
Upvotes: 26
Reputation: 25263
Our office works with e-learning apps built in Flash and we discovered early on that if the closing tag is omitted from the server side scripts then it breaks our communication mechanism with the e-learnng apps.
One caveat to this would be that our debuggng process didn't first try it in a simplified environment to make sure that it wasn't a combination of the missing closing tags and some erroneous output coming from some other script....
Upvotes: 0