Why omit the PHP closing tag?

Asked

Viewed 538 times

7

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

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

2 answers

11


A good reason is to prevent unwanted whitespace from appearing in our files, which may possibly cause the Error - Cannot Modify header information. As you can see in this question, there are several causes for this type of error that can go unnoticed by Ides.

Omitting the closing tag on purely PHP files is also a recommendation of PSR-2, 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 reference that is used in various places is even better.

Finally, it follows a passage from 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, thus unwanted blank space will not appear at the end of the files, and you will still be able to add headers to answer after. Also is useful if you use buffering output, and you do not want to have added a blank space at the end of the parts generated by files included.

3

As quoted in the PHP manual itself http://php.net/manual/en/language.basic-syntax.instruction-separation.php

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, thus unwanted blank space will not appear at the end of the files, and you will still be able to add headers to answer after. Also is useful if you use buffering output, and you do not want to have added a blank space at the end of the parts generated by files included.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.