0

Possible Duplicate:
PHP closing tag

Why pure php source files are suggested not to end with ?> ?

Today I found a problem that was caused by the ?>, which make the code print a extra char(maybe space, I am not sure)

Community
  • 1
  • 1
user1337896
  • 1,081
  • 1
  • 10
  • 15
  • For what it is worth: I think it is a very bad habit to omit the closing tag, even while ZEND devs advocate for it. Simply add them if you desire, but be sure nothing follows (spaces, tabs, newlines, etc) the closingtag. – Erwin Moller Oct 08 '12 at 10:55
  • @Erwin: I don't agree - actually it's good practice to omit it. The PHP interpreter does not need it and you avoid problems with started output due to hidden characters (headers can't be modified afterwards) and the like. – acme Oct 08 '12 at 10:57
  • 1
    @acme: yes, I know. But that is no (good) reason to omit it. In my opinion a programmer who cannot check if there is output after the closing tag should find another job. – Erwin Moller Oct 08 '12 at 11:00
  • 1
    Sometimes it's just the IDE that's inserting a linebreak or you accidently hit the return key - I doubt this never happened to you so far. You'll notice this very fast and fix the problem but this is just a unnecessary possible obstacle. And if things are making a programmers life easier without any drawbacks why shouldn't we use them? I don't think this is a question about being a good coder or not. – acme Oct 08 '12 at 11:53
  • @amce: I also see no reason to apply eschewing-workarounds when there are [actual solutions](http://fossil.include-once.org/phptags/index). – mario Oct 08 '12 at 12:16

1 Answers1

2

If you include the ?> at the end of the file there is a possibility that you might accidentally have a newline character afterwards. Many editors automatically add a new line at the end of the file, so it is easy for this to accidentally happen. The new line character will be sent to the client and this can give problems. In particular, the headers cannot be modified once content has been sent to the client.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • PHP eats up a newline. Just one though. The one trailing newline from editors alone wouldn't be a problem. It's most usually user ineptitude shuffling other whitespace there. – mario Oct 08 '12 at 12:12