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

Post by feyd »

Considering I have only had one error in the several thousand scripts I've written that was created by some extraneous text after a closing tag gives me little reason to switch. .. And that error was in my first set of scripts.

Shouldn't we write our scripts properly and use headers_sent() anyways? Just like shouldn't we write our scripts to work without register_globals, be prepared for magic_quotes being on and off, properly handle errors instead of hiding them with "@," short tags being off, and all the other things in the "best practice" category we've built through all the work this community has done?
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Instead of all of them, I'll just respond to what was directed towards me.
ole wrote:
...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?
Thing is, I've never (yes, you read that correctly) had a bug related to closing php tags (and introducing unwanted output).
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

All arguments either way do not change the fact that it was recently confirmed by Zend that the closing '?>' at the end of a file when there is no further output -- is optional and that they have chosen to not use it. That's just information to use as you see fit.

For me, it is of no consequence and is what I consider part of the range of exciting coding standards topics I file under "the art of programming". ;)
Last edited by Christopher on Thu Aug 31, 2006 1:34 am, edited 2 times in total.
(#10850)
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

here's a good reason for using ?> in my opinion. it gets you in practice of closing your php tags for those times your mixing HTML and PHP (spare me that war, just saying :-D ) :-D
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

To me it just makes sense. If you open a { you close it, if you open a ( you close it. If you open a [ you close it. Logically, if you open a <?php you close it ?>.
  • It is only two characters
  • It visually delineates a script
  • It eliminates potential mixed code errors
  • It maintains tabbed structure
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Everah wrote:To me it just makes sense. If you open a { you close it, if you open a ( you close it. If you open a [ you close it. Logically, if you open a <?php you close it ?>.
That's really not the point. Closing braces or parens or brackets is not optional -- you get an error. The fact that the ?> is optional if there is PHP code to the EOF is a feature of the language. No programmer's preference will change that fact and it is simply another bit of trivium we know about PHP to use as we see fit.
(#10850)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Based on what I remember reading, and I could be wrong, they only recommended doing that in class files.

I don't know what the big deal is though, as PHP will tell you what line and file sent the headers.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Well its obvious to me that I'm never going to changes some people's opinions here. Everyone seems to be justifing their current behaviour with the fact that the error is insignificant so I'll accept that.
they only recommended doing that in class files.
Something like "Files with exclusively PHP" so basically class files yep.
Shouldn't we write our scripts properly and use headers_sent() anyways?
headers_sent won't unsend the headers that are already sent, if you need to send headers and you can't there's a problem.
Thing is, I've never (yes, you read that correctly) had a bug related to closing php tags (and introducing unwanted output).
Considering I have only had one error in the several thousand scripts
You guys must have great memories to be so sure of that.
It is only two characters
So? Attention to detail, my friend.
To me it just makes sense. If you open a { you close it, if you open a ( you close it. If you open a [ you close it. Logically, if you open a <?php you close it ?>.
Are you worried that if you stop closing <?php you might start forgetting to close other things? Besides you're not stopping using ?> altogether.
as PHP will tell you what line and file sent the headers
And when that happens I would likely have to remove the ?> (not after looking through the whole file for echo, and all the functions that might print something) so it would be nice if it was never there in the first place.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

astions wrote:Based on what I remember reading, and I could be wrong,[...]
That's why would be nice to know the source ;)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

ole wrote:
To me it just makes sense. If you open a { you close it, if you open a ( you close it. If you open a [ you close it. Logically, if you open a <?php you close it ?>.
Are you worried that if you stop closing <?php you might start forgetting to close other things? Besides you're not stopping using ?> altogether.
Maybe that is the thing for me... I am not stopping use of the closing tag altogether. So it would be used here but not here. That, to me, just seems inconsistent. I know it it not required (so maybe my last post was not in the proper context) and I know it is only my preference (so no, you are not going to change my mind about :) ), but it just seems logical to include it. It hurts nothing if developed properly and allows for consistent coding throughout a project.

This is just my opinion, logical or not.
Last edited by RobertGonzalez on Thu Aug 31, 2006 11:31 am, edited 1 time in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

I've had this error due to copying a snippet from this forum. There was a space at the end which caused a header already sent error. Very easy to diagnose and fix.

As for me, doesn't matter. I do always use them. But I have noticed occasions where i've worked on an intense function and forgot the closing tag. The script still ran perfectly fine, which I thought was odd for a while.

So, in conclusion, I don't care. :oops:
Last edited by s.dot on Thu Aug 31, 2006 10:35 am, edited 1 time in total.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

yea I really don't care either. Not a big deal :? :roll:
Post Reply