Do PHP Files Require Closing Tags?

I don't know about you – but I always learnt to open and close my PHP files properly:

<?php
// Included PHP File
// Code goes here
?>

And yet now I am learning for the very first time that the closing PHP tags are not necessary, and in fact in some cases is not recommended!

The first clue I received was when I downloaded the Zend Framework. I noticed that most of the files were missing the closing tags (?>). Then somebody raised the issue on the Zend Framework Mailing List and the penny dropped:

  • PHP files have apparently never required closing tags
  • Leaving out the closing tags helps prevent whitespace sneaking into the ouput and messing with functions like header(), session_register() functions etc.

Well, that is definitely a new thought for me – and not one I have ever read anywhere. It sounds like it will make it into the Zend Coding Conventions though, so take note 😉

Be Sociable, Share!

Comments are closed.