PHP Short Tags: A Discussion on Usability and Portability
The question of whether PHP short tags are acceptable has been a topic of debate among programmers. According to the official PHP documentation, short tags are generally not recommended due to their lower portability compared to full-length tags like "
The convenience of short tags, especially the shorthand echo tag "=", is undeniable. It allows developers to write code more concisely, reducing the number of characters required. However, there are several reasons why short tags may not be the best choice.
Portability Concerns
One of the primary reasons for avoiding short tags is their limited portability. While many servers do support short tags, there is no guarantee that all servers will. If code that relies on short tags is moved to a server that doesn't support them, it will fail to work correctly. This issue becomes more significant when sharing or distributing code with others.
Readability and Standards
Another concern with short tags is their potential impact on readability. Some argue that the shortened syntax can make code more difficult to read, especially for novice programmers. Additionally, the PHP Coding Standard strongly discourages the use of short tags, which can be seen as a violation of established coding norms.
Conclusion
While short tags may offer convenience in certain situations, their use should be carefully considered. Portability issues and readability concerns should be taken into account. For code that needs to be portable and readable by a wide range of programmers, it is generally recommended to use full-length PHP tags. However, for code intended for a specific environment where short tags are known to be supported, the shorthand echo tag "=" may be acceptable.
It's important to note that as of PHP 5.4, short tags for variable interpolation "= ... ?>" are supported everywhere, regardless of the general shorttags setting. This improvement has made short tags more reliable for use in portable code. However, it is still recommended to proceed with caution and consider the potential consequences of using short tags before relying on them heavily.
The above is the detailed content of Should You Use PHP Short Tags: A Balancing Act of Convenience and Portability?. For more information, please follow other related articles on the PHP Chinese website!