Thursday 30 November 2017

php - Why would one omit the close tag?

itemprop="text">

I keep reading it is poor practice to
use the PHP close tag ?> at the end of the file. The header
problem seems irrelevant in the following context (and this is the only good argument so
far):




Modern
versions of PHP set the output_buffering flag in php.ini
If output buffering
is enabled, you can set HTTP headers and cookies after outputting HTML because the
returned code is not sent to the browser
immediately.





Every
good practice book and wiki starts with this 'rule' but nobody offers good
reasons.
Is there another good reason to skip the ending PHP
tag?


itemprop="text">
class="normal">Answer



Sending
headers earlier than the normal course may have far reaching consequences. Below are
just a few of them that happened to come to my mind at the
moment:




  1. While
    current PHP releases may have output buffering on, the actual production
    servers
    you will be deploying your code on are far more important than
    any development or testing machines. And they do not always tend to follow latest PHP
    trends immediately.


  2. You may have
    headaches over inexplicable functionality loss. Say, you
    are implementing some kind payment gateway, and redirect user to a specific URL after
    successful confirmation by the payment processor. If some kind of PHP error, even a
    warning, or an excess line ending happens, the payment may remain unprocessed and the
    user may still seem unbilled. This is also one of the reasons why needless redirection
    is evil and if redirection is to be used, it must be used with
    caution.


  3. You may get "Page loading
    canceled" type of errors in Internet Explorer, even in the most recent versions. This is
    because an AJAX response/json include contains something
    that it shouldn't contain, because of the excess line endings in some PHP files, just as
    I've encountered a few days ago.


  4. If
    you have some file downloads in your app, they can break
    too, because of this. And you may not notice it, even after years, since the specific
    breaking habit of a download depends on the server, the browser, the type and content of
    the file (and possibly some other factors I don't want to bore you
    with).



  5. Finally, many PHP
    frameworks including href="http://symfony.com/doc/current/contributing/code/standards.html"
    rel="noreferrer">Symfony, href="http://framework.zend.com/manual/1.10/en/coding-standard.php-file-formatting.html"
    rel="noreferrer">Zend and Laravel (there is no mention of this in the href="https://github.com/laravel/framework/blob/master/CONTRIBUTING.md#coding-guidelines"
    rel="noreferrer">coding guidelines but it follows the suit) and the href="http://www.php-fig.org/psr/psr-2/" rel="noreferrer">PSR-2 standard
    (item 2.2) require omission of the closing tag. PHP manual itself ( href="http://php.net/basic-syntax.instruction-separation"
    rel="noreferrer">1, href="http://php.net/manual/en/language.basic-syntax.phptags.php"
    rel="noreferrer">2), href="http://make.wordpress.org/core/handbook/coding-standards/php/#remove-trailing-spaces"
    rel="noreferrer">Wordpress, href="https://www.drupal.org/coding-standards#phptags"
    rel="noreferrer">Drupal and many other PHP software I guess, advise to do
    so. If you simply make a habit of following the standard (and setup href="https://github.com/fabpot/PHP-CS-Fixer" rel="noreferrer">PHP-CS-Fixer
    for your code) you can forget the issue. Otherwise you will always need to keep the
    issue in your
    mind.




Bonus: a
few gotchas (actually currently one) related to these 2
characters:




  1. Even some
    well-known libraries may contain excess line endings after
    ?>. An example is Smarty, even the most recent versions of
    both 2.* and 3.* branch have this. So, as always, watch for third party
    code
    . Bonus in bonus: A regex for deleting needless PHP endings: replace
    (\s*\?>\s*)$ with empty text in all files that contain PHP
    code.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...