Last Updated: February 25, 2016
·
1.261K
· moak

Omit the final closing ?> to avoid unwanted white-space leakage

Sometimes when creating custom code you might include a file, such as this.

<?php
class  MyClass {
    function foo(){
         //some code
    }
}
?>  

Looks fine.
And for whatever reason you might need to do a redirect, or modify some headers just to get warning: Cannot modify header information - headers already sent ...

What happened was there was trailing white-space after the final ?> started sending data back to the client.
One solution is to use <a href="http://php.net/manual/en/book.outcontrol.php">Output Buffering Control</a> to control when the output gets sent. However there are other situations when unwanted white-space might cause malformed markup language.

So in order to avoid this it is a good convention to omit the final closing ?> of a PHP file

<?php
class  MyClass {
    function foo(){
         //some code
    }
}