SitePoint Sponsor

User Tag List

View Poll Results: Do you?

Voters
78. You may not vote on this poll
  • Yes

    70 89.74%
  • No

    4 5.13%
  • I didn't know you could do that!

    4 5.13%
Page 1 of 3 123 LastLast
Results 1 to 25 of 54
  1. #1
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Do you close your PHP tags?

    I was first introduced to this crazy concept when I started some work with Nathan. Now I've seen multiple programmers do it. So, when you write a class or something, do you use both your opening (<?php) and closing (?>) tags?

    I do.

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,804
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    I thought it was just standard to close what you open?
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Same here.

  4. #4
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I never tried by not closing the tag ... and infact I didn't know as well that it works even if u don't close that. Anyway, it makes more sence to close if u open something, isn't it ?

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow I had no idea you didn't "have" to close to the opening tag, but I always close it.

  6. #6
    Now available in Orange Tijmen's Avatar
    Join Date
    Jul 2004
    Location
    The Netherlands
    Posts
    1,469
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I always use both of them.

  7. #7
    SitePoint Evangelist
    Join Date
    Sep 2004
    Location
    Oregon
    Posts
    445
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's proper. You are taught to do so in classes, and some programmers like to have PHP then close their PHP tags, and run pure HTML. I personally haven't been to a programming class, nor run pure HTML after my PHP.

  8. #8
    SitePoint Enthusiast shref's Avatar
    Join Date
    Nov 2004
    Location
    Egypt, Alexandria
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    iam closing it always.
    if you didn't close it you won't get errors
    Shreef
    blog: shreef.com
    twitter: @shreef

  9. #9
    SitePoint Guru mwolfe's Avatar
    Join Date
    Mar 2005
    Posts
    912
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah i've found that mixing php/html isnt a very elegant way of doing things.. especially if you use includes and things for outputting html.. In a small/basic site its not a big deal, but i typically use functions that return strings.. its much more flexible that way.. if you ever needed to, you could parse that string and do things with it that would be impossible had you just output it from the other file/function.. And of course you should always close php tags as if you don't you can easily end up with problems using header or setting cookies.

  10. #10
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mwolfe
    yeah i've found that mixing php/html isnt a very elegant way of doing things.. especially if you use includes and things for outputting html.. In a small/basic site its not a big deal, but i typically use functions that return strings.. its much more flexible that way.. if you ever needed to, you could parse that string and do things with it that would be impossible had you just output it from the other file/function.. And of course you should always close php tags as if you don't you can easily end up with problems using header or setting cookies.
    How does not adding a "?>" at the end of your file give you problems with headers or cookies?

    Anyway, I guess I'm the only one who doesn't bother adding the ?>. It's always seemed like a waste.

  11. #11
    SitePoint Guru mwolfe's Avatar
    Join Date
    Mar 2005
    Posts
    912
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    your right, i was thinking of if you had extra whitespace but you werent in a php block, theoretically this hsould be fine then to not close your php tags.. i suppose there isnt really any reason to close them..however i pretty much always do.

  12. #12
    SitePoint Member
    Join Date
    Oct 2005
    Location
    Portland, UK
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I always close them, I find it easier to read that way.

  13. #13
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by someonewhois
    How does not adding a "?>" at the end of your file give you problems with headers or cookies?

    Anyway, I guess I'm the only one who doesn't bother adding the ?>. It's always seemed like a waste.
    Look at sweatje's code example (link at the bottom of the page).
    http://www.phparch.com/shop_product.php?itemid=96

    He doesn't use them, either.

  14. #14
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool. I've never actually seen anyone else leave them off.

    Now, find someone who calls functions as "function(arguments)" but if statements as "if (expression)" (with a space). That'll a little more rare.

  15. #15
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe some people view the opening <?php as kind of like a perl or ruby script where one must do:

    #!/usr/bin/perl
    or
    #!/usr/bin/ruby

    Just a thought as to why anyone might leave off the last tag other than for laziness

  16. #16
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It seems logical to me. If I'm including a class, why would I close the PHP tag at the end of the class? I'm not leaving PHP, why should I close the tag?

  17. #17
    SitePoint Addict Quaint's Avatar
    Join Date
    May 2004
    Location
    Netherlands
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's just plainly absurd not to use them ?!?!

    "Why bother??", why bother writing good code at all? Why not forget about indenting, commenting, etc...

    Writing GOOD code is about writing maintainable code. A lot of PHP programmers tend to forget this! Whereas for instance Java/C++ programmers are always 'aware' of what they are doing, a lot of PHP programmers just put stuff in their program untill it works and don't care about the correctness of their code..

    Quaint Tech
    - Blog on web development and web technology.

  18. #18
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Quaint
    It's just plainly absurd not to use them ?!?!

    "Why bother??", why bother writing good code at all? Why not forget about indenting, commenting, etc...

    Writing GOOD code is about writing maintainable code. A lot of PHP programmers tend to forget this! Whereas for instance Java/C++ programmers are always 'aware' of what they are doing, a lot of PHP programmers just put stuff in their program untill it works and don't care about the correctness of their code..
    True but who says that closing your tag is "bad" code? At the beginning of this topic I thought 100% it was bad practice but what "someonewhois" said is true. It could help signify that a piece of code is inserted into another PHP file rather than being a standalone script. Or it could be because <?php to some people merely signifies that the following is PHP (and ?> is just another language construct to them, maybe, that "this is the end of PHP and beginning of something else")

    Sure, it seems like bad practice, but it doesnt necessarilly make dirty code such as indenting, commenting, and naming variables correctly.

    Why does PHP not raise a warning or a notice at least (or even a fatal error!) that such an event is occuring?

  19. #19
    SitePoint Addict
    Join Date
    Aug 2004
    Location
    Chicago
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When I'm writing a quick, dirty script that I delete right away, I don't even think about it. (I forgot one time, and when I realized it, I was like "OMG!!1!2!! 2 bytes saved")

    But otherwise, I always close it.
    Why's (Poignant) Guide to Ruby
    learn ruby with foxes, wizards, and chunky bacon

  20. #20
    SitePoint Evangelist nsj's Avatar
    Join Date
    Oct 2005
    Location
    Jamaica (W.I)
    Posts
    447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bjcffnet
    When I'm writing a quick, dirty script that I delete right away, I don't even think about it. (I forgot one time, and when I realized it, I was like "OMG!!1!2!! 2 bytes saved")

    But otherwise, I always close it.
    Being calculative... That's a good one!

    Anyway, what's the conclusion guys? Is there a standard? Following XHTML rules, i think we should close them, but then again, what does XHTML have to do with this?

  21. #21
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by someonewhois
    It seems logical to me. If I'm including a class, why would I close the PHP tag at the end of the class? I'm not leaving PHP, why should I close the tag?
    Why would you open the tag either?

  22. #22
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    Brooklyn, NY
    Posts
    359
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Although I always close my PHP blocks, there are many expert PHP developers who do not. It's not just a matter of being lazy - it's to avoid superfluous output that might be generated due to have whitespace between the closing PHP tags and the EOF. PHP automatically strips newlines that immediately follow the closing PHP tag to alleviate this problem, but I (and many others) find this terribly annoying:

    http://shiflett.org/archive/151
    Chris Shiflett
    http://shiflett.org/

  23. #23
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah yes that seems like a logical decision because I have many times gotten the "headers already sent" error or so when there has been whitespace after my closing tag

  24. #24
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by shiflett
    Although I always close my PHP blocks, there are many expert PHP developers who do not. It's not just a matter of being lazy - it's to avoid superfluous output that might be generated due to have whitespace between the closing PHP tags and the EOF. PHP automatically strips newlines that immediately follow the closing PHP tag to alleviate this problem, but I (and many others) find this terribly annoying:

    http://shiflett.org/archive/151
    Heh... That brings up another point. According to PEAR standards, you should have a blank line at the end of all of your files (after that closing tag, of course ). I've also had a C++ compiler spit at me once when I didn't leave a blank line at the end of the file. (I never touched C++ again, by the way.)

    Who came up with this stupid idea? A blank line at the end of the file pisses me off. That is a waste.

  25. #25
    SitePoint Guru mwolfe's Avatar
    Join Date
    Mar 2005
    Posts
    912
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    same thing with c. why the hell do they need a blank line at the end of a file is beyond me.. i'm sure there is some reason for it though.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •