PHP offers several opening and closing tag formats, including short tags and ASP style tags. While short tags may seem convenient for developers, official documentation advises against their use due to portability issues.
Despite this recommendation, many servers support short tags, making the shorthand syntax tempting to use. For instance, the syntax = is more concise than
Prohibited Short Tags vs. Acceptable Shorthand Echo Tags
The PHP Coding Standard prohibits the use of the short tag . This is primarily because its functionality depends on server settings, which can lead to compatibility issues if the code is moved to a server that does not support it.
In contrast, the shorthand echo tag = is universally supported and acceptable for use. This tag cannot be disabled and is therefore considered portable.
Arguments for Short Tags
Despite the recommendation against using short tags, some developers argue for their convenience. They claim that the shorter syntax reduces typing and improves code readability. However, it's important to note that most modern development environments offer syntax highlighting, negating this argument.
Considerations for Portability
As of PHP 5.4, the = ... ?> tag is supported everywhere, regardless of shorttag settings. However, if you need to support PHP versions prior to 5.4 and cannot guarantee that short tags are enabled, you should continue to use the full syntax .
Future Compatibility
It's worth noting that ASP tags (<%, %>, <%=) and script tags will be removed from PHP 7. For long-term portable code, consider transitioning away from these tags to maintain compatibility with future versions of PHP.
The above is the detailed content of Should I Use PHP Short Tags: Weighing Pros and Cons?. For more information, please follow other related articles on the PHP Chinese website!