Is it acceptable to use PHP short tags?
P粉882357979
2023-07-31 17:44:33
<p>According to information from official documentation: </p>
<blockquote>
<p>There are four different pairs of opening and closing tags available in PHP. Two pairs (<?php ?> and <script language="php"> </script>) are always available. The other two pairs are short tags and ASP style tags, which can be turned on and off in the php.ini configuration file. Although some people find short tags and ASP-style tags convenient, they are less portable and generally not recommended. </p>
</blockquote>
<p>In my experience, most servers have short tags enabled. Enter </p>
<pre class="brush:php;toolbar:false;"><?=</pre>
<p>Better input</p>
<pre class="brush:php;toolbar:false;"><?php echo</pre>
<p>More convenient. Programmer convenience is an important factor, so why are short tags not recommended? </p>
I like <?=$whatever?> too much to give it up. I've never had a problem. I'll keep using it until it gives me trouble. Seriously, 85% of (my) clients have access to php.ini to enable them on rare occasions. The other 15% use major hosting providers, and almost all of them have enabled them. I like them.
Must clearly distinguish between PHP short tags (<?) and abbreviated echo tags (<?=).
The former is prohibited in the PHP coding specification, mostly out of common sense, as this would bring problems if you need to move your code to a server that doesn't support short tags (and can't enable it) A lot of trouble. As you said, many shared hosts support short tags, but not all. If you want to share your script, it's best to use the full syntax.
The abbreviated echo tag <?= cannot be disabled, so it can be used completely.
I agree that <? is easier for programmers than <?php, but batch find and replace is possible as long as you use the same form each time.
I don't think readability is a reason. Syntax highlighting options are available to most serious developers.
As ThiefMaster mentioned in the comments, as of PHP 5.4, the <?= ... ?> tag is supported everywhere, regardless of the short tag setting. This means they are safe to use in portable code, but it also means there is a dependency on PHP 5.4. If you want to support versions before 5.4 and the use of short tags cannot be guaranteed, you still need to use <?php echo ... ?>.
In addition, you need to know that in PHP 7, the ASP tags <%, %>, <%= and script tags are removed. So if you want to support long-term portable code and want to switch to the most modern tools, consider changing these parts of your code.