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

PhpCoding Style

Php Problem Overview


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

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

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.

Solution 3 - Php

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.

Solution 4 - Php

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.

Solution 5 - Php

CodeIgniter Framework suggests to omit closing tags for

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

You can read it here.

Solution 6 - Php

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?

Solution 7 - Php

  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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNaughty.CoderView Question on Stackoverflow
Solution 1 - PhpdhhView Answer on Stackoverflow
Solution 2 - PhpMetalsharkView Answer on Stackoverflow
Solution 3 - PhpbrismuthView Answer on Stackoverflow
Solution 4 - PhpSoufiane HassouView Answer on Stackoverflow
Solution 5 - PhpMÇTView Answer on Stackoverflow
Solution 6 - PhpjohnlemonView Answer on Stackoverflow
Solution 7 - PhpNafis AhmadView Answer on Stackoverflow