Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

up vote 19 down vote accepted

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)
share|improve this answer
    
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 Jul 5 '11 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. Jul 5 '11 at 12:35

No, they are wrong.

?> is optionnal 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 : http://framework.zend.com/manual/en/coding-standard.coding-style.html 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.

share|improve this answer

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: http://stackoverflow.com/questions/4410704/php-closing-tag and here : http://stackoverflow.com/questions/3219383/why-do-some-scripts-omit-the-closing-php-tag

share|improve this answer
1  
newbies or not... there simply is no reason (except for OCD cases) to have them –  Mchl Jul 4 '11 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 Jul 4 '11 at 22:16
1  
I cringe anytime I see ?> in files containing pure PHP. –  Caffeinated Aviator Jul 5 '11 at 2:52
    
@Kenneth: If they're trivial - I can't see them. The OCD part was meant as a joke. –  Mchl Jul 5 '11 at 6:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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