PHP Code Formatting – Closing Tag (?>) on PHP Files?

code formattingPHP

Some people swear by closing their PHP files with ?>, some say it's more optimized to leave it off.

I know that it's not essential to have it on there, I'm just wondering what the pros and cons are of doing this, and what best practice is.

Best Answer

It's not so much a matter of performance - parsing the trailing ?> is trivial and won't make any noticable difference at all, unless you're including a million files per second.

IIRC, php.net recommends NOT adding the ?>, and the reasons go something like this:

  • it's unnecessary
  • it is easy to accidentally add significant whitespace after the ?>, which will be output to the client, which in turn can lead to obscure 'headers already sent' errors (this happens when an included file contains whitespace, and you try to set a header after including that file)
Related Topic