PHP is one of the most frequently used programming languages in web development. One of the most important features of this language is that it offers different limitation methods. Let's take a brief look at PHP opening and closing tags:

  • <?php ?>
  • <?= ?>
  • <? ?>
  • <% %>

In order to use PHP more efficiently, it is necessary to understand the differences and importance of these tags. In this article, we will learn how to use PHP tags effectively.

Why Should We Not Use Short PHP Tags?

Short PHP tags can be more useful for ease and speed of coding. However, on most hosting and servers it is disabled by default in the PHP.ini configuration. In PHP.ini configuration for these short tags to work short_open_tag option must be activated. So the only method that will definitely work on every PHP server is . If you're considering deploying your PHP to a server you don't control, you can always <?php ?> You should use the approach.

Short PHP Echo Tag and PHP.ini

<?php echo() ?>

in PHP <?= tag in PHP configuration short_open_tag It can be used whether the option is enabled or not, so you can safely use this shortcut instead of the one above.
If you're just writing code for yourself and can control the PHP configuration you use, you may find short tags more useful. But remember that <? ?> tag may conflict with XML declarations. Also <% %> The tag is actually ASP style.

PHP Closing Tags and Potential Problems

Closing a pure PHP file (for example, a file containing only a class definition) ?> When adding the tag, be careful not to leave any blank lines behind. While the PHP interpreter can safely "execute" a single line break character after the closing tag, other line breaks may be output to the browser. If you're then writing HTTP headers, things can get even more confusing.

PHP PSR-2

The PSR-2 coding standard is part of the PHP PSR coding standards created to determine best practices in PHP. PSR-2 coding standards apply only to files containing PHP code. ?> recommends not using closing tags. The main reason for this recommendation is to prevent characters such as spaces or line breaks accidentally left after the closing tag from being sent to the browser. In PHP, ?> Any character following the tag is automatically considered part of the HTML output. This can be especially problematic when processing HTTP headers.

Unnecessary characters at the end of a PHP file can prevent HTTP headers from being processed correctly. In the HTTP protocol, any output makes it impossible to send headers because headers must be completed before the content is sent. This causes an issue known as headers already sent, which can affect actions such as starting a session, setting a cookie, or redirecting a page.

Therefore, PSR-2 recommends not using the closing tag in files containing only PHP code, in order to avoid such common errors and preserve the cleanliness of the code. This approach eliminates possible problems that may occur due to unwanted spaces or end-of-line characters at the end of the code and contributes to a more stable and error-free operation of PHP scripts.

PHP Tags and Compatibility

When writing web applications for older versions, closing ?> html with tags <!doctype> Be careful not to leave a new line between the tags. Older versions of IE may throw different errors if they encounter any spaces, including newlines, before the doctype declaration. This is not the case with newer versions of IE and other, more advanced browsers. (That is, for every browser other than IE.)