Why I don’t use closing PHP tags

I got a question by a fellow programmer last tuesday about why I don’t use closing PHP tags. First of all, PHP doesn’t require it, so you even save a few bits on your total filesize. But the most important reason is that it prevents additional whitespace that could really mess up your website or application.

Many years ago during the whole pageflip hype I was asked by a client to develop a Flash pageflip using frequently changing input for their catalog. I couldn’t convince him to present his catalog in a more web-friendly manner so I knuckled down. I decided to dynamically generate images based on the client’s content coming from a database. After I made a working proof-of-concept a colleague was asked to do some alterations and soon after I got a phone call. The pageflip didn’t work anymore. It turns out my colleague had added closing PHP tags and a newline in the file for the image creation resulting in a damaged image.

So remember, never ever add closing PHP tags if you don’t need to.

P.S.: The tag I’m talking about obviously is :

?>

5 Replies to “Why I don’t use closing PHP tags”

  1. The PHP manual has the following to say about this:

    Note: The closing tag of a PHP block at the end of a file is optional, and in some cases omitting it is helpful when using include() or require(), so unwanted whitespace will not occur at the end of files, and you will still be able to add headers to the response later. It is also handy if you use output buffering, and would not like to see added unwanted whitespace at the end of the parts generated by the included files.

    Perhaps this reference to the official PHP website will help while explaining it to your colleagues.

  2. Adding what damage it might cause would be helpful.

    It would solve one of the most common errors, that is header() errors.

Comments are closed.