以下是官方文件提供的信息:
有四对不同的
可以是开始和结束标记
用于PHP。其中两个<?php ? >
And <script language="php"> </script>,
总是可用的。另外两个
是短标签和ASP风格标签,和
可以打开和关闭从
Php.ini配置文件。因此,
而有些人发现短标签和
ASP样式标签方便,它们都是
便携性较差,而且通常不是
推荐。
根据我的经验,大多数服务器都启用了短标记。打字
<?=
比打字方便多了
<?php echo
程序员的便利性是一个重要因素,那么为什么不推荐他们呢?
我在寻找了这个主题的信息后读了这篇文章,我觉得有一个主要的问题没有被提及:懒惰vs.一致性。PHP的“真正”标签是<?PHP和?>。为什么?我真的不在乎。这些明明是PHP的,为什么还要用别的东西呢?<%和%>对我来说是ASP, <script .....意味着Javascript(在大多数情况下)。因此,为了一致性、快速学习、可移植性和简单性,为什么不坚持标准呢?
另一方面,我同意模板中的短标签(仅在模板中)看起来很有用,但问题是我们在这里花了这么多时间讨论它,可能会浪费很长时间来输入额外的三个字符“php”!!
While having many options is nice, it's not at all logical and it can cause problems. Imagine if every programming language allowed 4 or more types of tags: Javascript could be <JS or < script .... or <% or <? JS.... would that be helpful? In the case of PHP the parsing order tends to be in favor of allowing these things, but the language is in many other ways not flexible: it throws notices or errors upon the slightest inconsistency, yet short tags are used often. And when short tags are used on a server that doesn't support them, it can take a very long time to figure out what is wrong since no error is given in some cases.
Finally, I don't think that short tags are the problem here: there are only two logical types of PHP code blocks-- 1) regular PHP code, 2) template echoes.
For the former, I firmly believe that only <?php and ?> should be allowed just to keep everything consistent and portable.
For the latter, the <?=$var?> method is ugly. Why must it be like this? Why not add something much more logical?
<?php $var ?>
That would not do anything (and only in the most remote possibilities could it conflict with something), and that could easily replace the awkward <?= syntax. Or if that's a problem, perhaps they could use <?php=$var?> instead and not worry about inconsistencies.
在有4个打开和关闭标签的选项以及随机添加一个特殊的“echo”标签的情况下,PHP也可以在PHP .ini或.htaccess中有一个“自定义打开/关闭标签”标志。这样设计师就可以选择他们最喜欢的。但出于显而易见的原因,这太过分了。那么为什么要允许4+选项呢?
值得一提的是,从PHP 7开始:
短ASP PHP标签<%…%>消失了
短PHP标签<?如果short_open_tag设置为true,则>仍然可用。这是默认值。
从PHP 5.4开始,短打印标签<?=…?>总是启用的,不管short_open_tag设置如何。
摆脱第一个语言是件好事,因为它干扰了其他语言。
除了个人喜好,现在没有理由不使用短打印标签。
当然,如果您正在编写与PHP 5遗留版本兼容的代码,则需要坚持旧的规则,但请记住,PHP 5.6之前的任何东西现在都不受支持。
参见:https://secure.php.net/manual/en/language.basic-syntax.phptags.php
还要注意,上面的引用不鼓励使用第二个版本(<??>)因为它可能已经被禁用:
注意:
由于短标签可以被禁用,所以建议只使用正常标签(<?>和<?= ?>)以最大化兼容性。