Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PSR-2 coding standard: why no closing PHP tag in files containing only PHP? [duplicate]

I just heard of the PSR-2 coding standard in a comment on this question: Is there any reason to use the "public" keyword before method and member variable names?

I have a question on one of the rules in the PSR-2 standard:

The closing ?> tag MUST be omitted from files containing only PHP.

What is the point of that?

like image 891
developerwjk Avatar asked Jan 28 '26 05:01

developerwjk


2 Answers

It is a good universal rule not using closing tag in php scripts. Note that everything after that closing tag is sent to client (browser) even white characters so if you're using closing tag and new line or any other white character it will be sent to browser. In most cases this behavior is not desirable.

like image 191
Marcin Twardowski Avatar answered Jan 29 '26 18:01

Marcin Twardowski


To prevent issues from trailing whitespace http://hardcorewp.com/2013/always-omit-closing-php-tags-in-wordpress-plugins/

like image 37
MrGlass Avatar answered Jan 29 '26 18:01

MrGlass



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!