Questioning the Quirks of PHP
June 05, 2017
Resources aside, here are some quirks that I’ve found in my ongoing deep-dive into PHP. To make this list, the behavior has to be some level of strange, mysterious, and other-worldly with at least of few Stack Overflow questions starting with the words, why on earth.
Omit Closing Tags on Pure PHP Files
Early on in PHP fundamentals it’s taught that if a PHP file has only PHP in it, vs PHP & HTML, then the closing tag (
?>) is optional. In an attempt to be buttoned up, my inclination was to include them unless there was rationale behind the practice. SitePoint champions an explanation and includes a couple reasons.
The closing tag is not needed to run the file. Since it’s not necessary, and keeping code DRY is one of the key software principles, the tags should be omitted.
It can cause conflicts at run time. If the PHP closing tag is included and there is any whitespace or new line after it, then there will be an issue when importing that code in other files using
require. Since PHP will read until EOF, or end of file, then it will notice the whitespace and load it as part of the file. This is most notably a problem when trying to edit headers with PHP as any extra character after the closing tag will be sent to the page as content. Once the page receives content, the headers cannot be modified and instead will throw an error. Worse yet, if the production server has errors turned off, there will be a silent fail of the entire page.
More to Come
Written by Steve Frost who lives in Minneapolis using technology to make an impact in the community and our environment.Follow on Twitter