Why do some scripts omit the closing PHP tag, '?>'?

04/01/2020 08:00:01

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

Verified Answer (179 Votes)

07/10/2010 18:41:14

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.


Answer #2 (84 Votes)

07/10/2010 18:42:00

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.


Answer #3 (33 Votes)

10/18/2013 01:29:01

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.

Hack Hex uses Stack Exchance API by the Stack Exchange Inc. to scrape questions/answers under Creative Commons license.