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.


3 Answers 3


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)
  • Based on the answers here (particularly regarding the empty unwanted whitespace causing "headers already sent" errors), I have changed my habit of religiously including a ?> at the end of a PHP file. I noticed when I created a new file with PHPStorm the template just inserted a <?php without the ?> closing tag, and thought it was just sloppy coding. Now I know better.
    – tcrosley
    Commented Jul 5, 2011 at 0:46
  • This precise reason - "headers already sent" - was the reason this has been rigorously forbidden by my employer (a big web portal). It is one thing when you forget it at the end of a file you just wrote. It's another if you edit a library entry that is 3 includes deep, your text editor inserts the whitespace without you knowing, and suddenly a piece of portal ran by guys 2 buildings away stops working. The tree of includes is often 100+ files, finding the bug becomes an exercise in patience. Expect "deep gratitude" visit weeks later as the bug is found and traced back to you.
    – SF.
    Commented Jul 5, 2011 at 12:35
  • 1
    PHP sending white space after the closing tag is yet another example of poor language decisions.
    – user949300
    Commented Aug 21, 2017 at 23:23

No, they are wrong.

?> is optional in PHP at the end of a file. And you'll find good reason for this. The most important one is that an empty space at the end of a file will not prevent you from sending headers. This is a difficult bug to spot because you can find it in any file anywhere.

The usual way to do is putting a closing tag when PHP is mixed with HTML and not putting it for pure PHP files. It's even a coding standard from ZEND framework and many others.

Optimized means that the code run faster. This is easy to prove them wrong. Profile the code and figure out that they are telling you bullshit.


I think it is recommended to newbies to avoid adding it so that they don't cause extra newline chars to be sent accidentally. Since it is not essential to have it like you have mentioned, I think the general reasoning is that better leave it off to avoid mistakes.

I don't think there is any "optimization" involved with it.

I would point you to here: https://stackoverflow.com/questions/4410704/php-closing-tag and here : https://stackoverflow.com/questions/3219383/why-do-some-scripts-omit-the-closing-php-tag

  • 1
    newbies or not... there simply is no reason (except for OCD cases) to have them
    – Mchl
    Commented Jul 4, 2011 at 19:26
  • @Mchl I would point out that the reasons for leaving off are fairly trivial and in the end it just comes down to the preference of the programmer and its not really an OCD issue...
    – Kenneth
    Commented Jul 4, 2011 at 22:16
  • 1
    I cringe anytime I see ?> in files containing pure PHP. Commented Jul 5, 2011 at 2:52
  • @Kenneth: If they're trivial - I can't see them. The OCD part was meant as a joke.
    – Mchl
    Commented Jul 5, 2011 at 6:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.