Page 1 of 1

Include File PHP end tag?

Posted: Mon Aug 25, 2008 2:20 am
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?

Re: Include File PHP end tag?

Posted: Mon Aug 25, 2008 2:42 am
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!

Re: Include File PHP end tag?

Posted: Mon Aug 25, 2008 3:58 am
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.

Re: Include File PHP end tag?

Posted: Mon Aug 25, 2008 4:09 am
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
 
 

Re: Include File PHP end tag?

Posted: Mon Aug 25, 2008 4:10 am
by onion2k
Yep. Although not with the opening carriage return before the <?php tag. :)

Re: Include File PHP end tag?

Posted: Mon Aug 25, 2008 4:29 am
by jaoudestudios
Ofcourse :)

Re: Include File PHP end tag?

Posted: Mon Aug 25, 2008 5:00 am
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?

Re: Include File PHP end tag?

Posted: Mon Aug 25, 2008 5:44 am
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.

Re: Include File PHP end tag?

Posted: Fri Aug 29, 2008 4:24 pm
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

Re: Include File PHP end tag?

Posted: Fri Aug 29, 2008 4:46 pm
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().

Re: Include File PHP end tag?

Posted: Fri Aug 29, 2008 4:46 pm
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.