以下是官方文件提供的信息:
有四对不同的
可以是开始和结束标记
用于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+选项呢?