25 April 2007

PHP Programming: Why You Should Omit ?> If It Occurs at the End of The File

Posted under: at 23:55

Every single PHP documentation will tell us that in order to escape from HTML to PHP mode, we need to start with <? or <?php and end with ?>. What they neglect to say is that we don’t really need ?> whenever it occurs at the end of the file.

In fact, it is better if we omit them.

We don’t need to put ?> at the end of the file because when PHP encounters the end of the file, it will revert back into HTML mode. And therefore putting ?> at the end of the file would be redundant.

It is more than that though. If we put ?> at the end of the file, sometimes we need to try very hard that there’s nothing between ?> and the end of the file. Or we might get the following error:

Warning: Cannot modify header information – headers already sent by (output started at /foo/bar/blah.php:57) in /foo/bar/baz.php on line 57

Sounds familiar? This probably occurs more often with whitespaces before end of file than straight programming errors. Not really hard to fix, but annoying nonetheless. By simply removing the ?> before the end of the file, we can avoid the problem altogether. We no longer need to hunt down those elusive invisible to the eye whitespaces. See?

Oh, and there’s more. Sometimes whitespaces will get there without you knowing, even after you ship the software. Consider these:

  • Your user edits your software’s config.php, he/she unknowingly adds a line break or more at the end of the file.
  • Text editors that ‘conveniently’ adds an ending line break when opening foreign text file format or a text file without terminating line break.
  • FTP client that ‘conveniently’ adds a line break whenever it is transferring text files without terminating line break in ASCII mode.

It might be unbeliveable, but a simple task of removing ?> that occur right before the end of every PHP file can and WILL reduce your support costs, however small it is.

That’s the reason why all my PHP program will not have ?> when it ‘should’ occur at the end of the file.

By removing all ?> that hugs the end of file, you will eliminate most if not all those ‘headers already sent’ support inquiries. However, if my experience is any indication, people will start ‘complaining’ that they get ‘incomplete’, ‘unfinished’ or otherwise ‘corrupted’ files. This is actually worse than those ‘headers already sent’ support inquiries. The solution is simple: just add a comment like # end of file or even # this is really really really really the end of this file, there's intentionally no PHP end marker here at the end of every PHP file.

43 Responses

Trackback: Use this URI to trackback this entry. Use your web browser's function to copy it to your blog posting.

Comment RSS: You can track conversation in this page by using this page's Comments RSS (XML)

Gravatar: You can have a picture next to each of your comments by getting a Gravatar.

Leave a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Warning: Comments carrying links to questionable sites will be removed!