tags:

views:

1060

answers:

10

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?

A: 

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....

Noah Goodrich
A: 

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.

CesarB
+15  A: 

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.

James Murray
I agree that the closing tag should be omitted from PHP-only files when possible. However, as others have posted there are situations (several) where not including a closing tag will cause the code to break.
ZCHudson
It's not entirely accurate that any whitespace after the ?> is a problem. A single new line character after the closing ?> is not a problem; it will be consumed by the parser and not echoed. This is for compatibility with MANY text editors which require a line break at the end of all lines.
thomasrutter
The problem is if you are using an Windoze editor, a <cr><lf> is inserted, which does cause http output to be emitted.
dar7yl
A: 

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.

weierophinney
How is stating what happened to me FUD? (I also saw several times the closing tag NOT be used, in particular Drupal never uses it, which is why I tried omitting it on that particular file, thinking it would not make a difference - even more since I had read that part of the manual.)
CesarB
And, in fact, I so didn't believe it would make a difference that the web developer had to insist several times for me to try adding the ?> before I did. After I added the closing tag, everything suddenly started working.
CesarB
Likewise, I began omitting the closing tag after having read about it in regard to security best practices. However, after having done so, we discovered that our e-learning apps began failing and putting the closing tags back in fixed it.
Noah Goodrich
Please note the caveat to my post which stated that we never fully debugged the problem so I can't and won't say for certain that omitting the closing tags was the real cause of the problem or if this only exposed a problem which was unknown.
Noah Goodrich
For reference: http://php.net/manual/en/language.basic-syntax.instruction-separation.php is the page that states the closing tag is optional.
outis
+1  A: 

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)

Kris
I just think that a php file is not an xml file... Don't run xml tools on a php file, run it on the output of the php file.
DGM
+3  A: 

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.

+4  A: 

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.

porneL
Excellent points
Tom
+3  A: 

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.

dar7yl
Omitting a closing php tag has nothing to do with writing html/xml. PHP is not either one of those.
DGM
However, the <?php ... ?> tag is usually imbedded within html, as most use is for web processing. PHP is defined within the context of HTML, otherwise, there would be no use for the tags.
dar7yl
A: 

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.

warren
+1  A: 

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.

Bob Fanger
I'd say the bottom one would probably be a better practice; I know that -l doesn't really do much interpretation of the files rather than basic syntax checking but I guess I'd be nervous about something in one file affecting parser state for the subsequent files.
thomasrutter
@thomasrutter True, you should use the second (slower) one. There are several problems with the first one. Duplicate classes and functions for example. But I consider duplicate definitions of classes and functions designflaws anyway ;)
Bob Fanger