Include File PHP end tag?
Moderator: General Moderators
-
mpetrovich
- Forum Commoner
- Posts: 55
- Joined: Fri Oct 19, 2007 2:02 am
- Location: Vancouver, WA, USA
Include File PHP end tag?
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?
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?
- jaoudestudios
- DevNet Resident
- Posts: 1483
- Joined: Wed Jun 18, 2008 8:32 am
- Location: Surrey
Re: Include File PHP end tag?
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!
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?
I disagree with this. It's better to leave the PHP tags unclosed. For example:jaoudestudios wrote:Close the php tags!
Code: Select all
<?php
//do some stuff
?>
- jaoudestudios
- DevNet Resident
- Posts: 1483
- Joined: Wed Jun 18, 2008 8:32 am
- Location: Surrey
Re: Include File PHP end tag?
So you would do this....?I disagree with this. It's better to leave the PHP tags unclosed
Code: Select all
<?php
// code
Re: Include File PHP end tag?
Yep. Although not with the opening carriage return before the <?php tag. 
- jaoudestudios
- DevNet Resident
- Posts: 1483
- Joined: Wed Jun 18, 2008 8:32 am
- Location: Surrey
Re: Include File PHP end tag?
Ofcourse 
-
mpetrovich
- Forum Commoner
- Posts: 55
- Joined: Fri Oct 19, 2007 2:02 am
- Location: Vancouver, WA, USA
Re: Include File PHP end tag?
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.jaoudestudios wrote:Returns are used in functions!
So, if we leave out the closing tag, is it better for processing?
Re: Include File PHP end tag?
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.mpetrovich wrote: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.jaoudestudios wrote:Returns are used in functions!
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.So, if we leave out the closing tag, is it better for processing?
-
mpetrovich
- Forum Commoner
- Posts: 55
- Joined: Fri Oct 19, 2007 2:02 am
- Location: Vancouver, WA, USA
Re: Include File PHP end tag?
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.
For example
Or if you want to temporarily remove an include file for development, just put a return in the first line
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.
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.(Using return) I can't think of a situation where I'd use it.
For example
Code: Select all
if ( $UserLevel < 10) return; // don't do the rest
//.. statements here for $UserLevel 10+Re: Include File PHP end tag?
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...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 exampleCode: Select all
if ( $UserLevel < 10) return; // don't do the rest //.. statements here for $UserLevel 10+
Code: Select all
if ($UserLevel < 10) {
//Do nothing
} else {
//.. statements here for $UserLevel 10+
}In that case I'd comment out the call to include().mpetrovich wrote:Or if you want to temporarily remove an include file for development, just put a return in the first line
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Include File 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.mpetrovich wrote:Funny, I was helping someone, just yesterday, and we traced the problem down to extra spaces after the PHP end tag.
(#10850)