Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Non-closing PHP tags in includes?

Contributor ,
Oct 03, 2010 Oct 03, 2010

Copy link to clipboard

Copied

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

TOPICS
Server side applications

Views

2.2K
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Oct 10, 2010 Oct 10, 2010

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

...

Votes

Translate
New Here ,
Oct 03, 2010 Oct 03, 2010

Copy link to clipboard

Copied

Hi

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

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 03, 2010 Oct 03, 2010

Copy link to clipboard

Copied

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.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Oct 03, 2010 Oct 03, 2010

Copy link to clipboard

Copied

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 03, 2010 Oct 03, 2010

Copy link to clipboard

Copied

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 :-).

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 10, 2010 Oct 10, 2010

Copy link to clipboard

Copied

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.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Oct 10, 2010 Oct 10, 2010

Copy link to clipboard

Copied

LATEST

Well, thanks a lot David and Nate. Now I know a little bit more about PHP...

I may use Zend instead of pure PHP, as used in David's book (Dreamweaver CS5 with PHP). I also bought the Zend Framework's official Programmer's Reference Guide (which is just a printed copy of the online documentation...).

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines