以下是官方文件提供的信息:

有四对不同的 可以是开始和结束标记 用于PHP。其中两个<?php ? > And <script language="php"> </script>, 总是可用的。另外两个 是短标签和ASP风格标签,和 可以打开和关闭从 Php.ini配置文件。因此, 而有些人发现短标签和 ASP样式标签方便,它们都是 便携性较差,而且通常不是 推荐。

根据我的经验,大多数服务器都启用了短标记。打字

<?=

比打字方便多了

<?php echo 

程序员的便利性是一个重要因素,那么为什么不推荐他们呢?


当前回答

短标签在某些web服务器(共享主机等)中默认是不开启的,所以如果你需要转移到这些服务器上,代码可移植性就成了一个问题。 对某些人来说,可读性可能是个问题。许多开发人员可能会发现<?PHP作为代码块的开始标记比<?当你扫描一个文件时,特别是当你被HTML和PHP紧密交织在一起的代码库困住时。

其他回答

如果你关心XSS,那么你应该使用<?= htmlspecialchars(…)?>大多数情况下,所以短标记不会有很大区别。

即使你将echo htmlspecialchars()缩短为h(),它仍然是一个问题,你必须记住几乎每次都要添加它(并试图跟踪哪些数据是预先转义的,这是不可转义的,但无害只会使错误更容易发生)。

我使用默认情况下安全的模板引擎,并写入<?PHP标签。

php中有3个标签:

<?PHP ?>不需要任何配置指令 Short_open_tag <??如果short_open_tag选项在 Php.ini已打开 缩短标签<?=自PHP 5.4.0以来,它总是可用的

从PHP 7.0.0删除asp和script标签

把< ?(不带尾随空格)到<?PHP(后面有一个空格):

find . -name "*.php" -print0 | xargs -0 perl -pi -e 's/<\?(?!php|=|xml|mso| )/<\?php /g'

把< ?(后面有空格)到<?PHP(保留尾随空格):

find . -name "*.php" -print0 | xargs -0 perl -pi -e 's/<\? /<\?php /g'

因为它会与XML声明产生混淆。不过,很多人都同意你的观点。

另一个问题是,如果用短标签编码所有东西,最后却发现最终的托管服务器已经关闭了它们,这会带来痛苦……

我在寻找了这个主题的信息后读了这篇文章,我觉得有一个主要的问题没有被提及:懒惰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+选项呢?