Include File PHP end tag?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
mpetrovich
Forum Commoner
Posts: 55
Joined: Fri Oct 19, 2007 2:02 am
Location: Vancouver, WA, USA

Include File PHP end tag?

Post by mpetrovich »

This must be addressed somewhere, but I could not find it in my searches.

Is is better (or proper) to exclude the PHP end tag, "?>", at the end of an include file?

Would a "return" be better or even recommended before the end tag, if the end tag is used?

Does this save PHP processing from dropping out and then resuming?

I have been burnt before by having blank lines follow the end tag, which sends blank lines to the HTML page and starts the headers. But, I am really wondering what is best practice?
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Include File PHP end tag?

Post by jaoudestudios »

html will only display information that has been echo'ed out!

Close the php tags! Return is not needed on an include. Read php.net http://uk3.php.net/include/

Returns are used in functions!
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Include File PHP end tag?

Post by onion2k »

jaoudestudios wrote:Close the php tags!
I disagree with this. It's better to leave the PHP tags unclosed. For example:

Code: Select all

<?php
 
//do some stuff
 
?>
 
Note the extra carriage return after the closing ?> tag. If you were to save that as a file and then include it that carriage return would be sent to the browser. That would break all subsequent calls to things like start_session(), header(), etc. If you leave the ?> out then mistakes like that can't happen.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Include File PHP end tag?

Post by jaoudestudios »

I disagree with this. It's better to leave the PHP tags unclosed
So you would do this....?

Code: Select all

 
<?php
// code
 
 
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Include File PHP end tag?

Post by onion2k »

Yep. Although not with the opening carriage return before the <?php tag. :)
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Include File PHP end tag?

Post by jaoudestudios »

Ofcourse :)
mpetrovich
Forum Commoner
Posts: 55
Joined: Fri Oct 19, 2007 2:02 am
Location: Vancouver, WA, USA

Re: Include File PHP end tag?

Post by mpetrovich »

jaoudestudios wrote:Returns are used in functions!
Actually, returns CAN be used in include files, just like a function. That is actually quite useful. It lets you abort an include when needed.

So, if we leave out the closing tag, is it better for processing?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Include File PHP end tag?

Post by onion2k »

mpetrovich wrote:
jaoudestudios wrote:Returns are used in functions!
Actually, returns CAN be used in include files, just like a function. That is actually quite useful. It lets you abort an include when needed.
That's interesting. I've just run a little test and you're quite right, it does return to the point where the include call was made. I'm not sure how useful that would be mind you. I can't think of a situation where I'd use it.
So, if we leave out the closing tag, is it better for processing?
It'll make so little difference to processing you might as well ignore that aspect of it. PHP is very fast. Worrying about things that save/cost a couple of milliseconds is a waste of effort.
mpetrovich
Forum Commoner
Posts: 55
Joined: Fri Oct 19, 2007 2:02 am
Location: Vancouver, WA, USA

Re: Include File PHP end tag?

Post by mpetrovich »

Funny, I was helping someone, just yesterday, and we traced the problem down to extra spaces after the PHP end tag.

The reason I started this thread is I wanted to know if I should make leaving off the PHP end tag part of my standard practices. I had heard this was recommended by Zend, but could not find a reference.
(Using return) I can't think of a situation where I'd use it.
Examples of the use of returns within an include is endless. Using a return within an include is analogous to a 'break' in a for, foreach, while, do-while statement. Although, this is not something I use a lot, it can be handy.

For example

Code: Select all

if ( $UserLevel < 10) return;  // don't do the rest
//.. statements here for $UserLevel  10+
Or if you want to temporarily remove an include file for development, just put a return in the first line
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Include File PHP end tag?

Post by onion2k »

mpetrovich wrote:Examples of the use of returns within an include is endless. Using a return within an include is analogous to a 'break' in a for, foreach, while, do-while statement. Although, this is not something I use a lot, it can be handy.

For example

Code: Select all

if ( $UserLevel < 10) return;  // don't do the rest
//.. statements here for $UserLevel  10+
I don't like that much.. it's not obvious what happens. As this thread demonstrates people don't realise that return bails out of the include. I'd do something like...

Code: Select all

if ($UserLevel < 10) {
  //Do nothing
} else {
  //.. statements here for $UserLevel  10+
}
It's very obvious what's supposed to happen then.
mpetrovich wrote:Or if you want to temporarily remove an include file for development, just put a return in the first line
In that case I'd comment out the call to include().
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Include File PHP end tag?

Post by Christopher »

mpetrovich wrote:Funny, I was helping someone, just yesterday, and we traced the problem down to extra spaces after the PHP end tag.
This is the reason why many do not use the closing ?> in files containing only PHP. Even Zend does not use the closing tag because it eliminates the possibility of hard to find errors from whitespace after the tag. There is no downside really.
(#10850)
Post Reply