202

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.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Naughty.Coder
  • 3,922
  • 7
  • 32
  • 41
  • 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 '10 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](http://framework.zend.com/manual/en/coding-standard.php-file-formatting.html) – Halil Özgür Nov 15 '10 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 '10 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. – Anthony Hatzopoulos Oct 29 '12 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. – Andrew_1510 Nov 09 '12 at 10:02
  • @Halil Özgür your link to a Zend page ends in "Not Found". I searched the Zend forum, and could not find any reference to not using closing tags. End tags seem to only create a problem when using WordPress and Drupal templates. – RationalRabbit Jul 30 '19 at 07:36
  • @RationalRabbit sorry, it was not versioned. I believe the one intended at the time was: https://framework.zend.com/manual/1.10/en/coding-standard.php-file-formatting.html (more at https://stackoverflow.com/a/4499749/372654) – Halil Özgür Jul 31 '19 at 20:00

7 Answers7

191

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.

dhh
  • 4,289
  • 8
  • 42
  • 59
  • 124
    Some would also consider this to be a language defect. – D.Shawley Jul 10 '10 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. – Kieran Allen Jul 10 '10 at 13:48
  • 4
    It's is the [Zend](http://framework.zend.com/manual/en/coding-standard.php-file-formatting.html) and [Drupal](http://drupal.org/coding-standards#phptags) standard to omit the `?>` tag. – Josh Jan 20 '11 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. – Peter Johnson Jul 24 '11 at 17:01
  • 1
    @KieranAllen If you have other code (say, HTML markup) after the closing tag then the whitespace isn't an issue. – phatskat Aug 12 '13 at 14:02
  • 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 '14 at 09:24
  • meh...if we define 'defect' as 'standards spec I don't like in my pedantic world' then it's a defect. Otherwise, there certainly are pros--it's saved me many times – jpmorris Mar 25 '14 at 20:01
  • @Gaetan, in any well defined language grammar is not any issue missing a "closing something" until it is defined in the grammar. For example it is not mandatory having always "else" after every "if" – CoffeDeveloper Feb 25 '15 at 20:39
  • 3
    @Joe "Otherwise, there certainly are pros--it's saved me many times" To say this is to not understand the objection. It saved you many times, yes. But you only needed saving because of the language defect. So saying it saved you is not an argument against that it is a language defect. This is like saying, "the guy next door stopped the himself from shooting me. Therefore he didn't hold a gun to my head." – ahnbizcad Oct 30 '15 at 01:16
  • @ahnbizcad. No its not a defect just because you say it is sorry but you need a definition that is better than 'what I say goes'.. And given that its not a defect...or at least you haven't defined it any better than 'I don't like this thing' then we have clear advantages for use. So its kinda like 'I don't like that the guy next door has a gun. He must be defective.' Not only do you have to do better than that. But its still pretty awesome he didn't shoot anyone. – jpmorris Oct 30 '15 at 07:34
  • @Joe indeed, the lines of syntax vs good/bad language design become blurred at a point. Maybe that's what you're getting at, if I understand you correctly. If so, I concur. I guess I strongly feel that making closing tags optional is bad design. Language features and idiosyncracies can be arbitrary, but there is good design and bad design. While things may become arbitrary and subjective if you break it down enough, it seems difficult to say that certain languages' apis are simply better than others in certain specific respects. (e.g. consistency, conciseness, performance, etc). – ahnbizcad Oct 30 '15 at 20:09
  • @ahnbizcad Exactly. You're fine to define 'bad' design however you want. Now feel free to point to consistency. I like consistency. But unlike missing closing some bracket somewhere in your code, in this case you get a free win if you don't, since it can save you. So we have a clear win, and the only loss we have is the PREFERENCE choices of the OCD-inclined (oh my god there's no closing tag, I like everything at right angles, no!) Give me a break. Either way, the point is that it is not a 'defect' (unless you define 'defect' in a non-agreed-upon way). And there's a win to this 'defect'. – jpmorris Nov 01 '15 at 04:58
  • the benefits are undisputed. It is the design of having to do this to achieve that benefit that is being argued against by others and myself imo. In other words, if the compiler would automatically not blow up over whitespaces after the closing tag, that would be "better" design. In that respect, I think people mean that omitting the closing tag in PHP is a language "defect". Perhaps a fair comparison might languages having / not having garbage collection. Does the one that doesn't have it have a "defect"? I see your point about not calling it a defect. I guess some ppl consider it to be one. – ahnbizcad Nov 01 '15 at 17:08
  • Not a defect, sometimes you need to use the closing tag because you have content you want to output directly after it. User error, not the language. Although you won't be using it much (at all?) in "modern" PHP it's still totally a useful part of the language. – John Hunt Feb 26 '16 at 09:00
  • the PHP interpreter interprets anything NOT in those tags as HTML. It's not a language defect at all, it's expected behavior. It's perfectly valid to end a php tag, have HTML, and then start using php again in the same document. And you get much better syntax highlighting if your html isn't wrapped in a echo statement as a string. – Native Coder Oct 06 '16 at 15:29
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.

Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116
Metalshark
  • 8,194
  • 7
  • 34
  • 49
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.

brismuth
  • 36,149
  • 3
  • 34
  • 37
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.

Soufiane Hassou
  • 17,257
  • 2
  • 39
  • 75
8

CodeIgniter Framework suggests to omit closing tags for

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

You can read it here.

MÇT
  • 1,007
  • 13
  • 21
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?

Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116
johnlemon
  • 20,761
  • 42
  • 119
  • 178
  • Hum, not really - with output buffering enabled theres no need to omit closing tags imho. – dhh Feb 17 '11 at 07: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.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nafis Ahmad
  • 2,689
  • 2
  • 26
  • 13