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

有四对不同的 可以是开始和结束标记 用于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中启用短标签那样启用它。

一种稍有不同的情况是在开发CodeIgniter应用程序时。CodeIgniter似乎在模板/视图中使用PHP时使用短标签,否则在模型和控制器中它总是使用长标签。这并不是框架中的硬性规则,但在大多数情况下,框架和许多来自其他用途的源代码都遵循这一约定。

我的意见?如果您从未打算在其他地方运行这些代码,那么如果您愿意,可以使用它们。当我意识到这是一个愚蠢的想法时,我宁愿不做大量的搜索和替换。

<?PHP ?>更适合使用,因为这种编程语言的开发人员已经大量更新了他们的核心语言。你可以看到短标签和长标签之间的区别。

短标签将突出显示为浅红色,而较长的标签将突出显示为深色!

然而,回显某些内容,例如:<?=$variable;?>很好。但更喜欢长标签。<?PHP回显$variable;>

如果有人还在关注这个…PHP 5.4.0 Alpha 1 <?=总是可用的:

http://php.net/releases/NEWS_5_4_0_alpha1.txt 所以看起来短标签(a)是可以接受的,(b)是可以保留的。至少现在……

http://uk3.php.net/manual/en/language.basic-syntax.phpmode.php上有很多建议,包括:

而有些人发现短标签和 ASP样式标签方便,它们都是 便携性较差,而且通常不是 推荐。

and

如果您正在嵌入PHP,请注意 在XML或XHTML中 使用<?保留PHP ?>标记 符合标准。

and

应该避免使用短标签 在开发应用程序或 用于 重新分配,或者在PHP上部署 不属于你的服务器 控件,因为短标记可能不是 目标服务器支持。为 可移植的、可重新分发的代码 不要使用短标签。