PHP files – Closing the PHP tag or not?

Allot of people think that a PHP file without a closing tag, is not also strange but wrong and won’t compile. But that’s far from the truth. In fact, even Zend (the company behind the PHP language) forbids its use in the Zend Framework.

Here’s an example:

<?php

function foobar ($foo) {
    echo $foo;
    return 0;
}

Most programmers in a file like this, would put a ?> in the end. It’s ok. But in PHP-only files, it’s not recommended.

How come this is a good practice? Well, first of all, the PHP interpreter won’t complain about a missing closing tag. And second, the most important, is that leaving the file without a closing tag will avoid accidental injection of trailing whitespace into the response.

Quoting Zend manual:

The closing tag of a PHP block at the end of a file is optional, and in some cases omitting it is helpful when using include() or require(), so unwanted whitespace will not occur at the end of files, and you will still be able to add headers to the response later. It is also handy if you use output buffering, and would not like to see added unwanted whitespace at the end of the parts generated by the included files.

Off course that there are people radically disagreeing with this. They point out that leaving the PHP tag open is programming lazyness (although I don’t see how anyone can be lazy about writing two characters) and they also state that it’s fear, it’s an “easy and safe solution”. Well the “easy” part it makes some sense, but the “safe” one?! Oh, ok… let’s just fire all the security conscious programmers! Who needs security? Security is bad. Not. PHP already has a very bad reputation because of his kind of attitude.

I’m sure there are better arguments against this, but my opinion is to leave it open, don’t close the tag, it can bring unnecessary problems. It won’t harm your PHP code and won’t make you lose hours trying to debug some invisible white spaces.


 
 
 
  • ** Update **
    Just for all the others that are facing the hours and hours of debuging the osCommerce php store fronts code... I just simple removed the closing tag and presto!! no more nasty.

    "warning cannot modify header information headers already sent by output started at..."

    Errors

    Whoo hooo

    Lenn Dolling
    Skyboard Software
  • I am currently facing errors due to this trailing spaces issue almost every time I add a extra feature to the osCommerce system that I am using.... I will delete all the closing php tags and pray it will solve all this bugs that I am getting...
  • lars
    For me, the laziness issue applies more to how the white space is getting in there more than the 2 characters at the end of the file. If the white space could be avoided by proper programming, than this practice could condone lazy programming and by Zend mandating it, they are perhaps used to sloppy coders using PHP. If it's an issue of file system (I never use PHP on Windows for example, so I don't know if that environment is more prone to this problem), then that's another issue altogether.

    PHP is not a mark-up language so there is no real comparison to HTML (and I'm not sure what the comparison to CSS is referring to), but in any case there _are_ optional tags in HTML -- but not in XHTML. For a (non-asp) programmer the oddity starts even with the need for an opening tag: basically the only need for the tags is to provide a convenience in order for us to mix PHP with HTML (or other mark-up or scripting languages) without a lot of "print/ln" and "echo" statements. If you want to see extra white space, have a look at the html source of almost any jsp output.
  • Hi, well I believe that for those who not use PHP so often but usually use html, css ... this idea of not closing tags is very strange. In html and css you always have to close a Tag so... using a language that you don't need it...

    For me that part were you say "even Zend (the company behind the PHP language) forbids its use in the Zend Framework" it's enough.
blog comments powered by Disqus