Skip to main content
Inspiring
October 3, 2010
Answered

Non-closing PHP tags in includes?

  • October 3, 2010
  • 2 replies
  • 2304 views

Hi,

This post is directly targetted at David Powers, who is often here (but, of course, could interest others)...

I'm currently reading the book "Adobe Dreamweaver CS5 with PHP Training From The Source" and I just came accross a strange affirmation:

on page 184, in the "Why the Next Page Doesn't Always Load" sidebar, in the third bullet you state: "If the include file contains only PHP code, remove the closing PHP tag. This is not only legitimate, it's the recommended best practice."... jaw dropping... I've never seen that...

I need more explanations... Why would it be recommended? Wouldn't it break the code (particularly PHP) since there's an opening tag but not a closing one? That means that we could have a lot of opening tags without closing tags? Why would it be different from the code that is "inside" the page, for as far as I know it, includes "include" the code into the page "before running it"? So, doesn't PHP "sees" the same thing whether the code is "embed" in the page or included via the include statement?

Lot of questions, but I need more details, 'cause I'm not sure a Google research on that topic would give me any results...

Nelson

This topic has been closed for replies.
Correct answer David_Powers

Nate has already given you the explanation. If you want the chapter and verse, take a look at the following page in the PHP Manual: http://docs.php.net/manual/en/language.basic-syntax.instruction-separation.php. It says in part:

"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."

The Zend Framework Coding Standard for PHP goes much further on the the following page: http://framework.zend.com/manual/en/coding-standard.php-file-formatting.html

"For files that contain only PHP code, the closing tag ("?>") is  never permitted. It is not required by PHP and omitting it  prevents the accidental injection of trailing white space into the response."

The "never permitted" applies only to files being submitted to the Zend Framework.

If anything comes after the PHP code in a file, you must use the closing tag. It's only when the file ends with PHP that you can omit it.

2 replies

Inspiring
October 4, 2010

Not using a closing tag for include files helps to prevent the possibility of accidental white space/returns after the closing tag, which would break any headers and such that require no previous output (and is hard to troubleshoot with lots of includes). PHP will self-close at end of file.

Inspiring
October 4, 2010

Thanks a lot Nate. I still hope David gives more precision/answers all the questions, but I understand more...

But, like I said, this means that if I don't close the php tags inside the "regular" page, it will also "auto" close it? So, it's better usually to close it ourself, but it's easy to do so in "our internal" code, but we have "less" control in includes, that's why it's better not to close it?...

Inspiring
October 4, 2010

If your main page ended in PHP, it should self-close too. Personally, I cringe a bit when I see the missing closing tag, but I've learned to live with it :-).

Participant
October 3, 2010

Hi

David Powers adresses that topic in his book PHP Object-Oriented Solutions.

He, within that environment, says the same I believe.