leaving ?> off the end of a php file

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

leaving ?> off the end of a php file

Post by Luke »

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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

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. :?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

what error is that?
User avatar
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

Post by Christopher »

The Ninja Space Goat wrote:how come this isn't necessary, and should I leave it off?
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..

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)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Zend recommends you leave them off because if you put one in followed by anything else (including whitespace) it causes the headers to be sent. Often before you want them to be.
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

I compromise by putting

Code: Select all

#?>

at the end of my file :)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

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.
Agreed. Like Feyd said, it doesn't look complete to me..
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

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:

Code: Select all

if(0 == $var)
  echo 'Boo';
Over

Code: Select all

if($var == 0)
  echo 'Boo';
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...
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

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.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

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.
I wrote:...it doesn't look complete to me..
That's all the reason I need.

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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

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.
Riiiight.. that's going to help push me to drop it. :roll:
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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.
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.
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

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_onces
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

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.
...it doesn't look complete to me..
That's all the reason I need.
And the fact that is could introduce a bug into your code stands for nothing?
feyd wrote:Riiiight.. that's going to help push me to drop it.
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.
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'?
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 anyway :P You should really wash them too.
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.
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.

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)
@sweatje: I've no idea what you are saying there.

Oh btw thank you everybody I was really tired a moment ago and this has perked me right up :D
Post Reply