4

Looking into code of few sites I noted that some php files have ?> at the end of file and some doesn't. Doesn't it matter - and if it does, when must I put ?> at the end of the file and when mustn't I?

klm123
  • 12,105
  • 14
  • 57
  • 95

3 Answers3

27

Never use ?> at the end of the file.

It's entirely optional but including it provides the opportunity to slip whitespace into the output by accident. If you do that in a file that you include or require before you try to output headers then you'll break your code.

Putting ?> at the end of a PHP file has only drawbacks.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
3

Putting ?> is optional if it is the last PHP tag. Omitting it in the last tag will benefit you in one case. There may be case where text editor append hidden special character after that tag. This could create problem. Omitting last closing tag will solve this problem.

Nilambar Sharma
  • 1,732
  • 6
  • 18
  • 23
1

From PHP Official Documentation - 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.

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268