Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skipping PHP end tag [duplicate]

Tags:

php

end-tag

While I was developing with Magento, I found out that I don't need to put php end tag (?>) if I don't use HTML below PHP code. Is it safe and why don't we just put end tag?? Is it useful??

like image 613
Moon Avatar asked Feb 15 '10 02:02

Moon


2 Answers

The official stance:

Note: 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.

like image 125
deceze Avatar answered Oct 16 '22 17:10

deceze


It's useful when creating class files / code files, as it's very easy to add an extra space or newline at the end of a file, which can mess up output buffering header() output. Since PHP treats an EOF like a closing ?> in a file there's no danger in relying on the EOF.

like image 45
leepowers Avatar answered Oct 16 '22 19:10

leepowers