[drupal-devel] no ?> needed at the end of modules
Hi! Can anyone (Goba?) confirm or not confirm that ?> is unnecessary at the end of included PHP files (like Drupal modules)? If it turns out that it is not needed, I think we need to adapt this practice because this avoids that some whitespace at the end of cause havoc. I tested and this works with PHP4 but I'd like to see something more "official" on this. Regards NK
Can anyone (Goba?) confirm or not confirm that ?> is unnecessary at the end of included PHP files (like Drupal modules)?
I prefer that it remains, regardless whether it is needed or not. PHP coding everywhere, for any application, needs to have both the <? and ?>. Having PHP developers come to Drupal with an admonition to not close their tags is dirty, anti-learning, and case-scenario-ish. -- Morbus Iff ( you are nothing without your robot car, NOTHING! ) Culture: http://www.disobey.com/ and http://www.gamegrene.com/ O'Reilly Author, Weblog, Cook: http://www.oreillynet.com/pub/au/779 icq: 2927491 / aim: akaMorbus / yahoo: morbus_iff / jabber.org: morbus
On Fri, 19 Aug 2005 20:37:36 +0200, Morbus Iff <morbus@disobey.com> wrote:
Can anyone (Goba?) confirm or not confirm that ?> is unnecessary at the end of included PHP files (like Drupal modules)?
I prefer that it remains, regardless whether it is needed or not. PHP coding everywhere, for any application, needs to have both the <? and ?>. Having PHP developers come to Drupal with an admonition to not close their tags is dirty, anti-learning, and case-scenario-ish.
But you do not deny that it solves the whitespace issue. I do not think code style should win over a problem, but... the list will decide.
Can anyone (Goba?) confirm or not confirm that ?> is unnecessary at the end of included PHP files (like Drupal modules)?
I prefer that it remains, regardless whether it is needed or not. PHP coding everywhere, for any application, needs to have both the <? and ?>. Having PHP developers come to Drupal with an admonition to not close their tags is dirty, anti-learning, and case-scenario-ish.
But you do not deny that it solves the whitespace issue. I do not think code style should win over a problem, but... the list will decide.
I agree that it will solve the whitespace problem, but at too great a cost. -- Morbus Iff ( you are nothing without your robot car, NOTHING! ) Culture: http://www.disobey.com/ and http://www.gamegrene.com/ O'Reilly Author, Weblog, Cook: http://www.oreillynet.com/pub/au/779 icq: 2927491 / aim: akaMorbus / yahoo: morbus_iff / jabber.org: morbus
But you do not deny that it solves the whitespace issue. I do not think code style should win over a problem, but... the list will decide.
I agree that it will solve the whitespace problem, but at too great a cost.
I also don't think this has anything to do with code style. These are examples of code style: $innerUpper = "whee!"; if ($two_spaces) { $indent++; } What you're suggesting is equivalent to: $innerUpper = "whee!" if ($two_spaces) { $indent++; where the terminator is missing. That's not code style. -- Morbus Iff ( you are nothing without your robot car, NOTHING! ) Culture: http://www.disobey.com/ and http://www.gamegrene.com/ O'Reilly Author, Weblog, Cook: http://www.oreillynet.com/pub/au/779 icq: 2927491 / aim: akaMorbus / yahoo: morbus_iff / jabber.org: morbus
I'm completely in Károly's camp. For as long as I can remember, PHP treats <?php and ?> as separate markers: one to 'enter PHP mode' and one to 'leave PHP mode'. In practice, there is no requirement at all that they match up. This makes sense if you consider that the vast majority of Drupal files only have PHP code in them, and not HTML-with-embedded-PHP. Only the PHPTemplates have that really. At least for the settings.php, we should get rid of the ending tag because so many people have problems with whitespace after the last tag caused by nasty editors. For the other files, it doesn't matter much, but I wouldn't care one bit if we removed it there too. Steven
I am with Steven and Károly completely. I am one of the webmasters at php.net, where we have a view source link for people to examine and possibly learn from the site code. Go to http://www.php.net/source.php and click through the include file links in the sidebar. You will notice that (with only one poor exception), all the files are present without a closing PHP delimiter. It is not needed, and helps us avoid "headers already sent" errors, regardless of what intelligent or unintelligent IDE is used to edit those files. And of couse we (both php.net webmasters and PHP Manual contributors) encourage that practice. Some RTFM: http://php.net/basic-syntax.instruction-separation By the way, modules maintained by myself do not include a trailing closing PHP delimiter, look at eg bbcode.module source. Regards, Gabor Hojtsy
I'm completely in Károly's camp. For as long as I can remember, PHP treats <?php and ?> as separate markers: one to 'enter PHP mode' and one to 'leave PHP mode'. In practice, there is no requirement at all that they match up. This makes sense if you consider that the vast majority of Drupal files only have PHP code in them, and not HTML-with-embedded-PHP. Only the PHPTemplates have that really.
At least for the settings.php, we should get rid of the ending tag because so many people have problems with whitespace after the last tag caused by nasty editors. For the other files, it doesn't matter much, but I wouldn't care one bit if we removed it there too.
Steven
My introduction to Drupal was through Fantastico and CPanel; I edited the files in the browser using CPanel and *always* ran into the whitespace problem. If leaving off ?> would solve that, thus improving the user experience for many users (most of whom we probably never hear about), I'm all for it. I love symmetry as much as the next guy, but this is too easy and practical to say "no" to. -Robert
Wouldn't it be far more easy and practical to just enable output buffering, so that said whitespace as well as any debugging print lines don't cause problems? That way you still get the cleanliness of balanced delimiters but no "headers already sent" issue. On Saturday 20 August 2005 04:01 am, Robert Douglass wrote:
My introduction to Drupal was through Fantastico and CPanel; I edited the files in the browser using CPanel and *always* ran into the whitespace problem. If leaving off ?> would solve that, thus improving the user experience for many users (most of whom we probably never hear about), I'm all for it. I love symmetry as much as the next guy, but this is too easy and practical to say "no" to.
-Robert
-- Larry Garfield AIM: LOLG42 larry@garfieldtech.com ICQ: 6817012 "If nature has made any one thing less susceptible than all others of exclusive property, it is the action of the thinking power called an idea, which an individual may exclusively possess as long as he keeps it to himself; but the moment it is divulged, it forces itself into the possession of every one, and the receiver cannot dispossess himself of it." -- Thomas Jefferson
What about benchmarks? I suspect increased memory consumption, which Drupal already uses too much :) Goba
Wouldn't it be far more easy and practical to just enable output buffering, so that said whitespace as well as any debugging print lines don't cause problems? That way you still get the cleanliness of balanced delimiters but no "headers already sent" issue.
On Saturday 20 August 2005 04:01 am, Robert Douglass wrote:
My introduction to Drupal was through Fantastico and CPanel; I edited the files in the browser using CPanel and *always* ran into the whitespace problem. If leaving off ?> would solve that, thus improving the user experience for many users (most of whom we probably never hear about), I'm all for it. I love symmetry as much as the next guy, but this is too easy and practical to say "no" to.
-Robert
So far the only arguments *against* dropping ?> are aesthetics. It doesn't look good to people (because of how hard we've trained ourselves to spot things like unclosed brackets Arguments *for* dropping ?> include a real and tangible user-friendliness issue that probably can't be solved any other way without hurting performance (I'll still wait and see if anybody does benchmarks output buffering). Am I missing anything?
If you run a script and find you forgot to end a line with a semicolon, the solution is not to leave semicolons off every line of code, it's to go back and fix your error. If you find you put too much whitespace after ?>, get rid of it. Robert Douglass wrote:
So far the only arguments *against* dropping ?> are aesthetics. It doesn't look good to people (because of how hard we've trained ourselves to spot things like unclosed brackets
Arguments *for* dropping ?> include a real and tangible user-friendliness issue that probably can't be solved any other way without hurting performance (I'll still wait and see if anybody does benchmarks output buffering).
Am I missing anything?
Errors need to be fixed, language features can be of use. Goba
If you run a script and find you forgot to end a line with a semicolon, the solution is not to leave semicolons off every line of code, it's to go back and fix your error. If you find you put too much whitespace after ?>, get rid of it.
Robert Douglass wrote:
So far the only arguments *against* dropping ?> are aesthetics. It doesn't look good to people (because of how hard we've trained ourselves to spot things like unclosed brackets
Arguments *for* dropping ?> include a real and tangible user-friendliness issue that probably can't be solved any other way without hurting performance (I'll still wait and see if anybody does benchmarks output buffering).
Am I missing anything?
David, I'd be fine with that stance if the file we were talking about were a "developer" file, like file.inc. The patch that chx is going to roll will address settings.php only. It is the only file that really causes a problem. Furthermore, if the cause of your missing semicolon weren't programmer's error, but rather a mismatch between \n and \r on different OSs, and it kept popping up automatically every time a file created in one OS got loaded into a different OS, AND the damn thing wasn't necessary or mandated by the language to begin with, you might just leave it out. Especially if the people having problems are the non-programmers that don't even know about line endings and OSs and just want to make a website, and will be just as happy to use Wordpress if Drupal can't even survive a CPanel/Fantastico install. David Norman wrote:
If you run a script and find you forgot to end a line with a semicolon, the solution is not to leave semicolons off every line of code, it's to go back and fix your error. If you find you put too much whitespace after ?>, get rid of it.
Robert Douglass wrote:
So far the only arguments *against* dropping ?> are aesthetics. It doesn't look good to people (because of how hard we've trained ourselves to spot things like unclosed brackets
Arguments *for* dropping ?> include a real and tangible user-friendliness issue that probably can't be solved any other way without hurting performance (I'll still wait and see if anybody does benchmarks output buffering).
Am I missing anything?
On 19-Aug-05, at 2:46 PM, Morbus Iff wrote:
Can anyone (Goba?) confirm or not confirm that ?> is unnecessary at the end of included PHP files (like Drupal modules)? I prefer that it remains, regardless whether it is needed or not. PHP coding everywhere, for any application, needs to have both the <? and ?>. Having PHP developers come to Drupal with an admonition to not close their tags is dirty, anti-learning, and case-scenario-ish. But you do not deny that it solves the whitespace issue. I do not think code style should win over a problem, but... the list will decide. I agree that it will solve the whitespace problem, but at too great a cost.
Not that this is all all that unique, but we've found this a useful tool to find the extra white space in our php files, so thought I'd contribute it: $ find . -name \*.php | xargs -n 1 perl -0777ne 'print $ARGV,"\n" if ! /\?>$/s' $ find . -name \*.php -o -name \*.class -o -name \*.inc | xargs -n 1 perl -0777ne 'print $ARGV,"\n" if !(/\?>$/s) && !(/\?>[\r]$/s)' $ find . -name \*.php -o -name \*.class -o -name \*.inc | xargs -n 1 perl -0777ne 'print $ARGV,"\n" if !(/\?>[ \t]*$/s) && !(/\?>[ \t]*[\r]$/s)' I'm totally in favour of keeping both <?php and ?> - and yes, gotta keep vigilant against short tags too. Mike -- Mike Gifford, OpenConcept Consulting Free Software for Social Change -> http://www.openconcept.ca Latest launch Canadian Labour Congress / Congrès du travail du Canada http://canadianlabour.ca / http://congresdutravail.ca
Op vrijdag 19 augustus 2005 20:55, schreef Mike Gifford:
Not that this is all all that unique, but we've found this a useful tool to find the extra white space in our php files, so thought I'd contribute it:
I like this approach. And I vote against the -hackish- way of simply not using ?>. I have some standard relpacement expressions that i (sometimes forget to) run before I commit a file or a patch. It checks for * print_r, queryd ;) * ' ' lots of whitespace towards the end of the line * whitespace after ?> Can we not run such a fix-o-matic every now and then on the core and the contribs? Regards, Bèr -- [ Bèr Kessels | Drupal services www.webschuur.com ]
Not that this is all all that unique, but we've found this a useful tool to find the extra white space in our php files, so thought I'd contribute it:
I like this approach. And I vote against the -hackish- way of simply not using ?>.
It is not less hackish then using a trailing comma in an array definition. It is a feature provided by the language, and encouraged by the PHP Manual. $arr = array(1, 2, 3, 4, 5,);
I have some standard relpacement expressions that i (sometimes forget to) run before I commit a file or a patch. It checks for * print_r, queryd ;) * ' ' lots of whitespace towards the end of the line * whitespace after ?>
Can we not run such a fix-o-matic every now and then on the core and the contribs?
How would this integrate with CPanel? Goba
$arr = array(1, 2, 3, 4, 5,);
Note that var_export where "the returned representation is valid PHP code." exports arrays in this way. OK, back to topic. On memory consumption -- you do not need to benchmark that. It is trivial that you need additional memory to collect output. I'll submit a patch which removes ?> from settings.php to begin with. Regards NK
I can see from the arguments FOR removing the ending ?> that it has some practical uses. However, I am inclined to vote against this proposal. Are we relying on a documented feature of the language, or just behavior that happened to be there? If it is documented, then we can safely go for it. If not, then am a bit apprehensive of going with it. We can ask the PHP team if they are willing to formalise this behavior by a) committing to it in the future and b) documenting it. How about that?
Are we relying on a documented feature of the language, or just behavior that happened to be there? If it is documented, then we can safely go for it. If not, then am a bit apprehensive of going with it.
We can ask the PHP team if they are willing to formalise this behavior by a) committing to it in the future and b) documenting it.
How about that?
It IS documented, and IS encouraged by the PHP documentation. I already posted the RTFM link! I also explained how comitted the PHP website itself is towards this very practice: http://lists.drupal.org/archives/drupal-devel/2005-08/msg00681.html Goba
Here is the quote for those who did not click through + Note: The closing tag of a PHP block at the end of a file is optional, and in some cases + not using it is helpful when using output buffering and include() or require(). I think the matter is settled then. The only part left is whether this encourages sloppy programming for newbies, which is not a big deal I think, given that this is a language feature. On 8/21/05, Gabor Hojtsy <gabor@hojtsy.hu> wrote:
Are we relying on a documented feature of the language, or just behavior that happened to be there? If it is documented, then we can safely go for it. If not, then am a bit apprehensive of going with it.
We can ask the PHP team if they are willing to formalise this behavior by a) committing to it in the future and b) documenting it.
How about that?
It IS documented, and IS encouraged by the PHP documentation. I already posted the RTFM link! I also explained how comitted the PHP website itself is towards this very practice:
http://lists.drupal.org/archives/drupal-devel/2005-08/msg00681.html
Goba
On Sunday 21 August 2005 08:14 am, Khalid B wrote:
Here is the quote for those who did not click through
+ Note: The closing tag of a PHP block at the end of a file is optional, and in some cases + not using it is helpful when using output buffering and include() or require().
Can someone explain how it's helpful with output buffering and include()? Since with output buffering headers aren't sent until later anyway, it should make extra whitespace irrelevant, and therefore negate the need to leave off the ?> in order to permit sloppy whitespacing. -- Larry Garfield AIM: LOLG42 larry@garfieldtech.com ICQ: 6817012 "If nature has made any one thing less susceptible than all others of exclusive property, it is the action of the thinking power called an idea, which an individual may exclusively possess as long as he keeps it to himself; but the moment it is divulged, it forces itself into the possession of every one, and the receiver cannot dispossess himself of it." -- Thomas Jefferson
Here is the quote for those who did not click through
+ Note: The closing tag of a PHP block at the end of a file is optional, and in some cases + not using it is helpful when using output buffering and include() or require().
Can someone explain how it's helpful with output buffering and include()? Since with output buffering headers aren't sent until later anyway, it should make extra whitespace irrelevant, and therefore negate the need to leave off the ?> in order to permit sloppy whitespacing.
It is probably badly worded. It is generally helpful if you do - include(), require() *without* output buffering - anything *with* output buffering, not generating XML/SGML output Note that although HTML and XML is permissive on the whitespace you output, if you do generate some other format (ie. csv file, image, swf file) with output buffering, and store the results elsewhere, additional whitespace in unintentional places can be a big pain. It is actually irrelevant if you use output buffering or not. Goba
Note that although HTML and XML is permissive on the whitespace you output, if you do generate some other format (ie. csv file, image, swf file) with output buffering, and store the results elsewhere, additional whitespace in unintentional places can be a big pain. It is actually irrelevant if you use output buffering or not.
Neither HTML nor XML are permissive on leading whitespace. AFAIK HTML is invalid if there is leading whitespace before the <!DOCTYPE> while XML is invalid if the <?xml ?> prolog is not the very first thing in the file (mostly for encoding reasons). Yet another reason to get rid of ?>. Steven
On Friday 19 August 2005 01:45 pm, Karoly Negyesi wrote:
I prefer that it remains, regardless whether it is needed or not. PHP coding everywhere, for any application, needs to have both the <? and ?>. Having PHP developers come to Drupal with an admonition to not close their tags is dirty, anti-learning, and case-scenario-ish.
But you do not deny that it solves the whitespace issue. I do not think code style should win over a problem, but... the list will decide.
Like the E_NOTICE issue, the answer is to do things the "right way", because in the end that makes for cleaner and more maintainable code. Encouraging non-tight code is a bad practice. If stray space characters at the end of files is a problem and just deleting them isn't viable, then the "correct" solution is output buffering. I'm actually rather surprised that and curious why Drupal doesn't do output buffering for the whole page simply by default. That would actually make debugging a bit easier, too (at least under 4.6), since then print debug statements don't mess with header commands. :-) -- Larry Garfield AIM: LOLG42 larry@garfieldtech.com ICQ: 6817012 "If nature has made any one thing less susceptible than all others of exclusive property, it is the action of the thinking power called an idea, which an individual may exclusively possess as long as he keeps it to himself; but the moment it is divulged, it forces itself into the possession of every one, and the receiver cannot dispossess himself of it." -- Thomas Jefferson
Like the E_NOTICE issue, the answer is to do things the "right way", because in the end that makes for cleaner and more maintainable code. Encouraging non-tight code is a bad practice.
I think this is different from E_NOTICE - for me the answer is easy. Add a comment at the end of each file like /* php end tag omited to avoid whitespace */ I consider repsecting E_NOTICE way more important because this is a real functional issue, but thats probably off topic here. However for the most files I think it should not be a problem to get rid of them. And most editors I know even remove the end whitespace automatically, maybe optinal. Or do not care but show. Thomas
I'll cite PEAR Coding Standards as my basis for keeping ?> at the end of files. http://pear.php.net/manual/en/standards.file.php Karoly Negyesi wrote:
Hi!
Can anyone (Goba?) confirm or not confirm that ?> is unnecessary at the end of included PHP files (like Drupal modules)?
If it turns out that it is not needed, I think we need to adapt this practice because this avoids that some whitespace at the end of cause havoc.
I tested and this works with PHP4 but I'd like to see something more "official" on this.
Regards
NK
participants (11)
-
Bèr Kessels -
David Norman -
Gabor Hojtsy -
Karoly Negyesi -
Khalid B -
Larry Garfield -
Mike Gifford -
Morbus Iff -
Robert Douglass -
Steven Wittens -
Thomas Ilsche