16 Feb 2009, 4:38 p.m.

PHP Closing Tags Considered Harmful

It may be obvious to some, but this is a mistake I still see being made a lot, and I think it's high time we got over it.

Stop using PHP closing tags. It really is that simple, and here's why.

Before I elaborate, let's illustrate what I mean. In short, this is indubitably bad:


<?php

echo 'Hello World!';

?>

Whilst this is good:


<?php

echo 'Hello World!';


Of course, if you're embedding HTML in PHP, or vice versa, you'll need to use closing tags to demarcate the two languages. But given a file containing only PHP code, the closing tag is completely superfluous.

Worse, the closing tag is potentially harmful, since any whitespace, accidental or otherwise, after the tag will be sent to the browser as output, with potentially catastrophic effects:

  • Any output, even that tiny bit of whitespace, being sent to the browser will cause PHP to send the HTTP response headers. This means that if, later on in your script, you wish to specify an HTTP header - such as a redirect - or drop a cookie, this will fail. Instead you'll generate the "Cannot modify header information - headers already sent" warning message, with which every PHP developer will be more than familiar
  • If your output is in a fragile binary format, such as an image or SWF file, spurious whitespace has the potential to corrupt the file
  • If you're outputting markup, leading newlines will push your XML prolog or Doctype Declaration off the top line of the output, thus rendering your markup invalid. I've had first-hand experience of this issue preventing sites from displaying at all on some mobile devices, which tend to be a bit more temperamental than full Web browsers when it comes to parsing markup

In any of these cases, tracking down an errant fragment of whitespace in a large codebase will be more pain than you really want. That's why omitting the closing tag is part of the Zend Framework coding standards among others.

So there you go. The solution is simple: stop using PHP closing tags.

Posted by Simon at 01:53:00 PM
24 Nov 2009, 10:42 p.m.

Peter McDonald

Just recently started omitting the closing tag myself.

If you code carefully I see no reason not to include it but you are indeed correct it can stop a lot of headaches later.

9 Oct 2010, 3:55 p.m.

Neo

So you treat the symptom rather than the root source of the problem?

sorry but treating closing tags as evil just because of the carelessness of some devs (is making sure the file ends without a whitespace / line break such a hassle?) is just a kludge imo.

and yeah, i know that this "convention" is backed up by some big names, but then again, in the end it is just a hack, you are not solving the problem, just using a loophole in the language to prevent it.

http://choosetheforce.blogspot.com/2008/05/should-you-close-that-php-tag.html

18 Oct 2010, 2:47 a.m.

Simon [ADMIN]

Neo -

Clearly the root cause is people typing unnecessary characters for no reason.

And I assume that by "some big names", you mean "the coding standards of every single major framework and library out there".