`)?
" />
The Absence of the PHP Closing Tag: Beyond the Header Problem
It's widely recommended to omit the PHP closing tag (?>) at the end of PHP files. While the header issue may seem inconsequential in certain situations, there are additional compelling reasons for skipping this tag.
Potential Consequences of Omitting the Tag
Sending headers prematurely can have unforeseen consequences, including:
-
Server Compatibility: Production servers may not always adhere to the latest PHP trends, causing output buffering to be disabled.
-
Functionality Disruptions: Unexpected PHP errors or line endings can lead to issues with payment gateways, redirects, and other functionality.
-
Browser Errors: In Internet Explorer, AJAX responses and JSON includes with excess line endings can trigger "Page loading canceled" errors.
-
File Download Issues: Downloads can also be disrupted due to excess line endings.
-
Framework and Standard Compliance: Symfony, Zend, Laravel, and PSR-2 guidelines require the omission of the closing tag.
Additional Considerations
-
Third-Party Code: Some libraries, such as Smarty, may contain additional line endings after the closing tag.
-
Regex Cleanup: If desired, a regex can be used to remove unnecessary PHP endings from all relevant files.
The above is the detailed content of Should I Omit the PHP Closing Tag (`?>`)?. For more information, please follow other related articles on the PHP Chinese website!