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

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

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

<?=

比打字方便多了

<?php echo 

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


当前回答

让我们面对现实吧。没有短标记的PHP非常丑陋。

如果你无法访问php.ini,你可以在.htaccess文件中启用它们:

php_flag short_open_tag on

其他回答

我在寻找了这个主题的信息后读了这篇文章,我觉得有一个主要的问题没有被提及:懒惰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 5.4.0 Alpha 1 <?=总是可用的:

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

注意:从PHP 5.4开始,短标记<?=,现在总是可用。

由于Zend Framework在默认的MVC配置中引入了“PHP作为模板语言”,短标签又回来了。我不明白争论的是什么,你一生中生产的大部分软件都将在你或你的公司控制的服务器上运行。只要你能始终如一,就不会有任何问题。

更新

在Magento做了大量的工作之后,它使用了长格式。因此,我改用了长形式的:

<?php and <?php echo

over

<? and <?=

为了确保互操作性,似乎只需要做少量的工作。

当您使用具有单独视图文件的MVC框架或CMS时,使用它们是很好的。它速度快,代码少,不会让设计师感到困惑。只要确保您的服务器配置允许使用它们即可。