4

Possible Duplicate:
PHP closing tag

I've read recently that the ?> should be omitted from files that contain ONLY PHP

In fact even the Zend Framework code standard strongly discourages using ?> in files containing only PHP because:

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.

Is the injection of trailing white space really that bad? And it is really a hideous crime to not omit the ?> from files containing only PHP? It simply seems unnatural for me to do so.

Community
  • 1
  • 1
Sean
  • 6,389
  • 9
  • 45
  • 69
  • I hate that style, but I can see how it would make a difference in editors that automatically add a newline at the end of the file for some reason. – Ry- Feb 27 '12 at 15:15
  • 3
    The trailing whitespace is generally a cause of "header already sent" errors. Personally it seems like nonsense to me ever to omit the `?>`, but I'm protective of my whitespace and the loss of symmetry in `` burns me up. – Michael Berkowski Feb 27 '12 at 15:16
  • @zzzzBov That question is also a possible duplicate... – Max Rose-Collins Feb 27 '12 at 15:17
  • 2
    Hideous crime? No. Good practice to avoid easily overlooked errors? Probably. – swayne Feb 27 '12 at 15:17
  • @MaxRose-Collins, if you have an alternative, you should link to it. The one I linked to was from over a year ago. – zzzzBov Feb 27 '12 at 15:19
  • 1
  • Thank you for your opinions, didn't realise there was a duplicate :S didn't appear as a suggestion when I was writing this! I feel your pain too Michael, I'm a symmetry person myself, it just seems wrong to ommit ?> but at least now I have a clearer picture of why it was advised – Sean Feb 27 '12 at 19:24

3 Answers3

4

If you inject white space in an include and then try to use header() (or something else that depends on running before content is output) then you'll be entering debug hell. This is a quick and easy technique for avoiding that.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

If accidental injection happens you cannot send header or start session but errors will show you that. If that happens on every file you have then you have some problems

Vytautas
  • 3,509
  • 1
  • 27
  • 43
-1

As far as I know there are no code police running around chopping off people's hands for doing something like this. I say write good code and leave the ?> in.

JKirchartz
  • 17,612
  • 7
  • 60
  • 88