204

In some scripts I see that they omit writing a closing tag ?> for the script. Why is it and should I do this as well?

(I'm sure they have not forgotten it.)

7
  • 21
    Many older text editors always inject a trailing newline. And trailing whitespace led to "headers already sent" errors. The PHP interpreter actually circumvents this problem, and eats up a SINGLE trailing \r and \n newline after the ?> closing tag. Some unpracticed programmers however errornously added two or more trailing newlines or spaces, tabs after ?>. That's why it's considered good newbie guidance to omit the PHP close marker. It's however not indicative of good coding style.
    – mario
    Jul 10, 2010 at 14:33
  • 4
    @mario "It's however not indicative of good coding style." -> Not at all. Zend Framework (considered the most robust -and thus complex- by some) and many other professionals and organizations actually prohibits adding ?> to files unnedcessarily. Zend Framework Coding Standard PHP File Formatting Nov 15, 2010 at 14:36
  • 1
    @battal So do many other coding styles. I could counter argue with Horde or PEAR coding guidelines or CodeSniffer complaining about omitted tags. But in the end, every generalization is a lie. Declaring any one method as standard doesn't make it best practice. IMO use cases and developer proficiency should be determining here. (But yes, also stick to a chosen coding guideline!;)
    – mario
    Nov 15, 2010 at 14:55
  • 2
    It's just cooler to omit the closing tag. +1 for omitting it when you can. Like omitting semicolons in javascript when you know where and when it is ok and not needed. The hackers who exploit your code will do it too. It also saves a couple of bytes if you get off on that. I don't think it has anything to do with good coding style unless you plan on using eval() a lot on your php, which is bad practice. Oct 29, 2012 at 18:03
  • It might be bad adding the closing tag. It's one of good practice. But the truth is it's one of the quirks of PHP. It comes in history, and hard to fix. So follow this good practice when possible. Nov 9, 2012 at 10:02

7 Answers 7

192

Well, omitting the closing tag is just one solution for avoiding blanks and other characters at the end of file. For example any char which is accidentally added behind the closing tag would trigger an error when trying to modify header info later.

Removing the closing tag is kind of "good practice" referring to many coding guidelines.

15
  • 126
    Some would also consider this to be a language defect.
    – D.Shawley
    Jul 10, 2010 at 13:42
  • 6
    In some cases isn't it what is required? People are responsible for cleaning up the whitespaces after the ending tag. In some cases someone might require the output after the ending tag. Jul 10, 2010 at 13:48
  • 4
    It's is the Zend and Drupal standard to omit the ?> tag.
    – Josh
    Jan 20, 2011 at 17:35
  • 41
    All parsers must stop parsing at the EOF (End of file), so a closing tag is redundant and not a language defect. Jul 24, 2011 at 17:01
  • 17
    try any language and you have closing tag, if you don't have it at EOF, you have a syntax issue. Accepting unclosed block at EOF or printing garbage after closing tag are definitively two language defects.
    – Gaetan
    Feb 17, 2014 at 9:24
84

From PHP: Instruction Separation

The closing tag of a PHP block at the end of a file is optional, and in some cases omitting it is helpful when using include() or require(), so unwanted whitespace will not occur at the end of files, and you will still be able to add headers to the response later. It is also handy if you use output buffering, and would not like to see added unwanted whitespace at the end of the parts generated by the included files.

0
34

php.net on PHP tags:

If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects because PHP will start output buffering when there is no intention from the programmer to send any output at that point in the script.

15

They do it to avoid risking to have whitespaces after the closing tag which may stop headers to work.

This is, of course, true for PHP-only files.

8

CodeIgniter Framework suggests to omit closing tags for

"... can cause unwanted output, PHP errors or blank pages".

You can read it here.

5

Modern versions of PHP set the output_buffering flag in php.ini. If output buffering is enabled, you can set HTTP headers and cookies after outputting HTML, because the returned code is not sent to the browser immediately.

Are the examples still valid in this context?

1
  • Hum, not really - with output buffering enabled theres no need to omit closing tags imho.
    – dhh
    Feb 17, 2011 at 7:38
0
  1. It shows unwanted white space / blank page. HTTP headers do not work for those unwanted whitespace.
  2. Most JavaScript injection is made at the end of the file. It will show an error message and breaks the code, injected JavaScript code does not get executed.
2
  • 1
    It would be very bad if someone could inject JS at the end of the PHP file.
    – Calmarius
    Nov 22, 2013 at 19:59
  • 4
    Yea it will be. they can do lot of stuff with php, why should inject JS. I was fool then, and still one now. What was I thinking. Dec 1, 2013 at 16:45

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