Why omit the PHP closing tag?

7

Every good good practice book and wiki starts with this "rule" but no one offers good reasons.

What good reasons to ignore the ?> PHP closing tag?

    
asked by anonymous 04.05.2015 / 21:41

2 answers

11

One good reason is to prevent unwanted white space from appearing in our files, which may possibly cause the Error - Can not modify header information . As you can see from this question, there are several causes for this kind of error that may go undetected by IDEs.

Omitting the closing tag in purely PHP files is also a PSR-2 recommendation , a set of code-style rules followed by widely used PHP Frameworks such as Zend Framework, Symfony, Laravel, among others. Following a code style in our project results in a uniform code within the team and having a referral that is used in multiple places is even better.

Finally, a snippet of PHP documentation :

  

The closing tag of a PHP block at the end of a file is optional,   and in some cases omitting it is useful when using include or require, so   Unwanted white space will not appear at the end of the files, and   you will still be able to add headers to the reply afterwards. also   is useful if you use output buffering, and you do not want to have added   a space at the end of the parts generated by files   included.

    
04.05.2015 / 21:53
3

As quoted in the PHP manual itself link

  

The closing tag of a PHP block at the end of a file is optional,   and in some cases omitting it is useful when using include or require, so   Unwanted white space will not appear at the end of the files, and   you will still be able to add headers to the reply afterwards. also   is useful if you use output buffering, and you do not want to have added   a space at the end of the parts generated by files   included.

    
04.05.2015 / 21:50