Should You Close PHP Tags? The Debate Continues…

    Craig Buckler
    Craig Buckler
    Share

    I was amazed at the response to my recent article, Should You Close Your PHP Code Tags? I didn’t think it would be controversial or cause so much debate among developers! To recap, closing PHP tags (?>) are entirely optional if you’re coding a PHP-only file and there’s no intermingled HTML. Files containing single class definitions are good candidates. Omitting the closing tag has another benefit: it becomes impossible to accidentally add white space to the end of the file. Therefore, output cannot be sent to the browser before HTTP page headers are modified. Many respondents considered this to be a hack and stated that PHP programmers should simply fix their code. For others, omitting the closing tag prevented the possibility of errors — even if none ever existed. This can be especially useful for projects with many contributors using different IDEs. Respondents mostly fell into one of three groups:

    1. PHP developers, perhaps working on larger projects or teams, where omitting the closing ?> was useful, recommended, or a documented coding-standard.
    2. Coding purists, possibly with a background in XHTML or XML, who considered missing tags to be wrong.
    3. Developers from other disciplines who considered the discussion to be ridiculous!
    According to the SitePoint poll, a third of PHP developers did not use a closing tag, a third considered that bad practice, and third didn’t realize it could be omitted. For the record, I often omit the closing tag. It makes no difference to the PHP interpreter or the browser so there’s little reason to include it. However, I can appreciate why some developers dislike the coding style. Some interesting notions were raised. The first is that the interpreter could assume PHP code by default and allow escaping into HTML, e.g.
    
    // PHP file
    DoSomeStuff();
    <?html
    <p>output HTML</p>
    ?>
    DoMoreStuff();
    
    I suspect this idea is too revolutionary. It could possibly be added as a php.ini option, but it’d break existing applications and cause chaos. However, pure PHP files was an idea with more potential. In essence, you’d specify that a file contained PHP code only. This could be achieved by:
    • using a special file extension, e.g. filename.pphp, or
    • using a modified include/require function, such as
    
    include('filename.php', PHP_ONLY);
    
    If this were implemented, your PHP code wouldn’t require opening or closing tags. In addition, the functionality could be added without affecting existing PHP applications — it would be backward compatible. But, hey, it’s just an idea and may never go any further than this article. The PHP coding style debate will rage on…

    Frequently Asked Questions (FAQs) about PHP Tags

    Why is it recommended to omit the closing PHP tag in a file?

    The primary reason for this recommendation is to prevent the accidental inclusion of whitespace characters after the closing PHP tag. These characters can cause unwanted output, errors, or header redirection issues. It’s a common mistake that can be easily avoided by simply omitting the closing tag. PHP does not require an ending tag, and when a file ends with PHP code, the PHP interpreter will automatically assume an implied closing tag.

    What are the different types of PHP tags?

    PHP supports four types of tags: Canonical PHP tags, Short-open (SGML-style) tags, HTML script tags, and ASP-style tags. However, the use of some of these tags depends on the settings in the php.ini file. The most commonly used and recommended tag is the canonical PHP tag, which starts with ““.

    Can I mix HTML and PHP in the same file?

    Yes, you can mix HTML and PHP in the same file. PHP is designed to be embedded within HTML. When the PHP interpreter encounters a PHP opening tag, it starts interpreting the code as PHP until it finds a closing tag, after which it returns to outputting HTML.

    What happens if I forget to close a PHP tag?

    If you forget to close a PHP tag, the PHP interpreter will continue reading the file as PHP code until it reaches the end of the file or encounters another PHP opening tag. This could lead to unexpected results if you intended some of the code to be interpreted as HTML or text.

    Why do some PHP files start with a header comment?

    Some PHP files start with a header comment for documentation purposes. This comment often includes information about the file, such as the author, creation date, and a brief description of what the file does. This is not required, but it can be helpful for other developers who might work with the file in the future.

    What is the difference between the ‘echo’ and ‘print’ commands in PHP?

    Both ‘echo’ and ‘print’ are used to output data to the screen in PHP. The main difference between them is that ‘echo’ can take multiple parameters, while ‘print’ can only take one. Additionally, ‘echo’ is slightly faster than ‘print’.

    Can I use PHP inside JavaScript code?

    Yes, you can use PHP inside JavaScript code, but only if the JavaScript code is inside a PHP file. The PHP code will be executed on the server before the page is sent to the client, and the result of the PHP code will be embedded in the JavaScript code.

    What is the purpose of the semicolon in PHP?

    The semicolon is used to separate statements in PHP. Each statement in PHP must end with a semicolon. It tells the PHP interpreter that the current statement has ended and the next one is about to begin.

    Can I use PHP to interact with a database?

    Yes, PHP has extensive support for interacting with databases. It includes a number of built-in functions for connecting to a database, executing queries, and retrieving results.

    What is the difference between single and double quotes in PHP?

    In PHP, single quotes and double quotes are used to define strings. The main difference between them is that variables within double quotes will be parsed and replaced with their values, while variables within single quotes will be treated as literal text.