Thread: Closing tag

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    Kiev, Ukrain
    Posts
    89
    Rep Power
    2

    Closing tag


    I'm reading a manual about user registration now . And there is such lines :

    Code:
        // Note that it is a good practice to NOT end your PHP files with a closing PHP tag. 
        // This prevents trailing newlines on the file from being included in your output, 
        // which can cause problems with redirecting users.
    Сan u explain me this more wide ?
  2. #2
  3. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,136
    Rep Power
    1990
    Closing tags are optional for PHP files - as long as you're finishing with PHP code and not HTML code.

    The reason for this is explained extremely well in the message that you've quoted there. That's really the biggest reason for it.
  4. #3
  5. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    The biggest "problem" this practice solves is in relation to HTTP headers. HTTP headers have to be sent before any other content, even empty spaces.

    So say you have page.php that includes include.php:
    PHP Code:
    page.php:
    -------------------------------------------------------
    <?php
        
    include_once("include.php");
        
    header("Cache-Control: no-cache, must-revalidate");
        
    header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");

        
    // Other stuff here...
    ?>
     <- [ SPACE #1 ]
    -------------------------------------------------------

    include.php:
    -------------------------------------------------------
    <?php
        
    // Set variables or whatever
    ?>
     <- [ SPACE #2 ]
    -------------------------------------------------------
    SPACE #1 usually won't cause any real problem. It's unnecessary, but it's at the end of the process. All the headers have been sent that are going to be sent, etc.

    SPACE #2 is the problem one. When include.php is included, that space will be sent to the client. Immediately afterwards page.php tries to set headers. This will result in the headers not working and a PHP error message.

    So the concept is, if you simply leave the ?> off, you avoid this potential problem altogether.
    LinkedIn: Dave Mittner
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    Kiev, Ukrain
    Posts
    89
    Rep Power
    2
    Thank you for youor help .
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0

    Smile


    If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects because PHP will start output buffering when there is no intention from the programmer to send any output at that point in the script.

IMN logo majestic logo threadwatch logo seochat tools logo