leaving ?> off the end of a php file
Moderator: General Moderators
leaving ?> off the end of a php file
In a Zend Framework tutorial, I was advised to leave the end php tag ?> off the end of the file to prevent hard to debug errors... how come this isn't necessary, and should I leave it off?
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
It's a personal preference to me. I prefer to keep them on as it looks more complete. When mixing php and literal output you must use them, but technically the engine doesn't care as it will stop processing when it hits the end of the script anyways.
I've only run into the error they are referring to once.. and it wasn't all that hard to find.
I've only run into the error they are referring to once.. and it wasn't all that hard to find.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: leaving ?> off the end of a php file
Well ... it is necessary when you want to do what the "?>" tag does, which is exit PHP parsing mode. It is still necessary when embedding PHP within HTML/XML/etc..The Ninja Space Goat wrote:how come this isn't necessary, and should I leave it off?
However, just like it is common practice to not close connections because it just adds extra code and PHP will close everything when the script terminates -- likewise if you have a file with only PHP in it, then parsing will end at the EOF anyway, so it is extraneous.
PS - the error is if you have whitespace characters after the "?>" then headers will be sent because the whitespace is output.
(#10850)
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
-
d3ad1ysp0rk
- Forum Donator
- Posts: 1661
- Joined: Mon Oct 20, 2003 8:31 pm
- Location: Maine, USA
Agreed. Like Feyd said, it doesn't look complete to me..Everah wrote:Even after that recommendation, I still cannot bring myself to leave it off in my single developer apps. If it was a group project and there was not certainty over what was being included where, then I'd consider it. But in my apps, I always use it.
But hey, that's just me.
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
I never even considered the possibility that it could be left out...until I read the Zend documents...
In general, I consider it a good idea, although completely personal preference...it prevents a certain kind of error from occuring, but like Feyd said it's easy to locate, unless maybe you had a massive include directory...not sure if it points the file in question out...in which case it's obvious...
In anycase...IMHO it's completely personal preference...I now exclude it from scripts...as I don't like *any* HTML ever getting into my source files (either by accident or laziness)
Like using the following:
Over
To prevent assignment bugs...the principle is good, but aesthetically it looks wrong so I ignore the good practice here and go with the former instead of the latter...
Cheerio...
In general, I consider it a good idea, although completely personal preference...it prevents a certain kind of error from occuring, but like Feyd said it's easy to locate, unless maybe you had a massive include directory...not sure if it points the file in question out...in which case it's obvious...
In anycase...IMHO it's completely personal preference...I now exclude it from scripts...as I don't like *any* HTML ever getting into my source files (either by accident or laziness)
Like using the following:
Code: Select all
if(0 == $var)
echo 'Boo';Code: Select all
if($var == 0)
echo 'Boo';Cheerio...
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
I like leaving it out. One less thing to worry about, one less thing to bother doing.
But yeah when I first read that it could be left out I was a little surprised too.
Frankly guys if you don't have a good reason to continue using it, which none of you do, you should stop because you are just holding on to a habbit for the sake of it.
But yeah when I first read that it could be left out I was a little surprised too.
Frankly guys if you don't have a good reason to continue using it, which none of you do, you should stop because you are just holding on to a habbit for the sake of it.
-
d3ad1ysp0rk
- Forum Donator
- Posts: 1661
- Joined: Mon Oct 20, 2003 8:31 pm
- Location: Maine, USA
ole wrote:I like leaving it out. One less thing to worry about, one less thing to bother doing.
But yeah when I first read that it could be left out I was a little surprised too.
Frankly guys if you don't have a good reason to continue using it, which none of you do, you should stop because you are just holding on to a habbit for the sake of it.
That's all the reason I need.I wrote:...it doesn't look complete to me..
Should we all swap to
if (statement)
{
}
because we have no reason to use
if (statement){
}
Other than the fact that it looks better to (some of) us?
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Not to start a battle over this, but isn't that statement a little like saying 'since you haven't given a good reason to wear blue pants instead of black pants, you should not wear blue pants because it is a habit that you are holding on to for the sake of it'? Maybe people use those two characters because, for them, it makes sense to use it, they want to use it and it really is not causing any issues using it. Of all the 'Help me with this error message' threads I've seen around, I can't remember the last one in which the culprit was a closing PHP tag. Just my opinion.ole wrote:Frankly guys if you don't have a good reason to continue using it, which none of you do, you should stop because you are just holding on to a habbit for the sake of it.
If you are diligent about closing your files, and you do not run on a host with a php accelerator, you might be able to get a performance boost by concatinating all of your include files to a single file and only doing one include. The performance cost of parsing the extra unused PHP code might be less than all of the file stats you would perform on several require_oncesole wrote: Frankly guys if you don't have a good reason to continue using it, which none of you do, you should stop because you are just holding on to a habbit for the sake of it.
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Wow, I really <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span>-ed everybody off!
Very protective over your little ?> aren't you. Hehe.
DISCLAIMER: OK please note that I am trying my absolute best in this post to avoid a flame war. This is what I believe and you have your beliefs. We just have to respect each other whilst exchanging opinion and fact. Here goes.
You should really wash them too.
I agree that ?> completes a script but what is that really achieving? Cause it doesn't improve readability. You don't need something to tell you where the end of a file is. You are pretty much saying you like using it
Oh btw thank you everybody I was really tired a moment ago and this has perked me right up
Very protective over your little ?> aren't you. Hehe.
DISCLAIMER: OK please note that I am trying my absolute best in this post to avoid a flame war. This is what I believe and you have your beliefs. We just have to respect each other whilst exchanging opinion and fact. Here goes.
And the fact that is could introduce a bug into your code stands for nothing?That's all the reason I need....it doesn't look complete to me..
Just making an observation. It seems as though a new bit of thinking has come along and everybody is so firmly entrenched in their use of ?> that they won't listen to both good reasoning and, although I'm not sure how much this counts, a big company that knows its PHP.feyd wrote:Riiiight.. that's going to help push me to drop it.
No not really. Because in there case there is a disadvantage so wearing one kind of pants. Besides there are load of holes in those other others pants because you've been wearing them for so long anywayNot to start a battle over this, but isn't that statement a little like saying 'since you haven't given a good reason to wear blue pants instead of black pants, you should not wear blue pants because it is a habit that you are holding on to for the sake of it'?
True. This is a very small barely significant quibble but when you are writing large libraries (like me) it could become a problem. Besides you are actually having to go to extra effort to put a ?> in. Also I can remember myself positioning the ?> to make sure there wasn't space after and wasn't too much space before, all unnecessary.Of all the 'Help me with this error message' threads I've seen around, I can't remember the last one in which the culprit was a closing PHP tag. Just my opinion.
I agree that ?> completes a script but what is that really achieving? Cause it doesn't improve readability. You don't need something to tell you where the end of a file is. You are pretty much saying you like using it
- Because that's what you have always done.
- Because it completes a script visually. The advantage of is what exactly?
- Because it is soley a matter of perference. Which is 80% true but ignores a 20% that I believe is important. (Attention to detail is very important in programming)
Oh btw thank you everybody I was really tired a moment ago and this has perked me right up