Home >Backend Development >PHP Tutorial >Should You Avoid PHP Short Tags for Better Code Portability?
Are PHP Short Tags Indeed Unacceptable?
PHP offers various tag options for opening and closing PHP code. While short tags ( and ?>) and ASP-style tags (<% and %>) provide convenience, the PHP Coding Standard deems them "generally not recommended."
Reason for Discouragement
According to the PHP Coding Standard, short tags create portability issues since they may not be enabled on all servers. This can cause unexpected errors when transferring code to different hosts. Therefore, it's advised to use the full form, , for portability.
Exceptions
The shorthand echo tag (=), on the other hand, is fully supported and acceptable to use. This tag cannot be disabled and is included in PHP's core syntax.
Other Considerations
While short tags may offer convenience, readability and syntax highlighting are important factors. Many developers prefer the explicit
PHP Version Considerations
In PHP 5.4 and later, = ... ?> tags are supported universally. This means they are considered safe to use in portable code, assuming support for PHP 5.4 or higher. However, to support pre-5.4 versions, it's still necessary to use .
Modernization and Future Proofing
It's worth noting that ASP tags (<% , %> , <%= , and script tag) have been removed from PHP 7. While short tags remain functional, considering changing code to align with PHP 7 best practices is recommended for long-term compatibility and portability.
The above is the detailed content of Should You Avoid PHP Short Tags for Better Code Portability?. For more information, please follow other related articles on the PHP Chinese website!