可以使用 PHP 短标签吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/200640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are PHP short tags acceptable to use?
提问by MDCore
Here's the information according to the official documentation:
以下是官方文档中的信息:
There are four different pairs of opening and closing tags which can be used in PHP. Two of those,
<?php ?>and<script language="php"> </script>, are always available. The other two are short tags and ASP style tags, and can be turned on and off from the php.ini configuration file. As such, while some people find short tags and ASP style tags convenient, they are less portable, and generally not recommended.
PHP 中可以使用四对不同的开始和结束标记。其中两个,
<?php ?>和<script language="php"> </script>,始终可用。另外两个是短标签和ASP样式标签,可以从php.ini配置文件中开启和关闭。因此,虽然有些人觉得短标签和 ASP 样式标签方便,但它们的可移植性较差,一般不推荐使用。
In my experience most servers dohave short tags enabled. Typing
根据我的经验,大多数服务器确实启用了短标签。打字
<?=
is far more convenient than typing
远比打字方便
<?php echo
The programmers convenience is an important factor, so whyare they not recommended?
程序员的便利性是一个重要因素,为什么不推荐他们呢?
采纳答案by Oli
They're not recommended because it's a PITA if you ever have to move your code to a server where it's not supported (and you can't enable it). As you say, lots of shared hosts dosupport shorttags but "lots" isn't all of them. If you want to share your scripts, it's best to use the full syntax.
不推荐使用它们,因为如果您必须将代码移动到不支持它的服务器(并且您无法启用它),那么它就是一个 PITA。正如您所说,许多共享主机确实支持短标签,但“很多”并不是全部。如果您想共享您的脚本,最好使用完整的语法。
I agree that <?and <?=are easier on programmers than <?phpand <?php echobut it is possible to do a bulk find-and-replace as long as you use the same form each time (and don't chuck in spaces (eg: <? phpor <? =)
我同意这一点,<?并且<?=对程序员来说比<?phpand更容易,<?php echo但是只要您每次使用相同的形式(并且不要插入空格(例如:<? php或<? =),就可以进行批量查找和替换
I don't buy readability as a reason at all. Most serious developers have the option of syntax highlighting available to them.
我根本不买可读性作为理由。大多数认真的开发人员都可以选择语法突出显示。
As ThiefMaster mentions in the comments, as of PHP 5.4, <?= ... ?>tags are supported everywhere, regardless of shorttags settings. This should mean they're safe to use in portable code but that does mean there's then a dependency on PHP 5.4+. If you want to support pre-5.4 and can't guarantee shorttags, you'll still need to use <?php echo ... ?>.
正如 ThiefMaster 在评论中提到的,从 PHP 5.4 开始<?= ... ?>,无论短标签设置如何,都支持标签。这应该意味着在可移植代码中使用它们是安全的,但这确实意味着对 PHP 5.4+ 的依赖。如果你想支持 5.4 之前的版本并且不能保证短标签,你仍然需要使用<?php echo ... ?>.
Also, you need to know that ASP tags <% , %> , <%= , and script tag are removed from PHP 7. So if you would like to support long-term portable code and would like switching to the most modern tools consider changing that parts of code.
此外,您需要知道ASP 标签 <% 、 %> 、 <%= 和 script 标签已从 PHP 7 中删除。因此,如果您希望支持长期可移植代码并希望切换到最现代的工具,请考虑更改代码的那部分。
回答by Paolo Bergantino
I'm too fond of <?=$whatever?>to let it go. Never had a problem with it. I'll wait until it bites me in the ass. In all seriousness, 85% of (my) clients have access to php.ini in the rareoccasion they are turned off. The other 15% use mainstream hosting providers, and virtually all of them have them enabled. I love 'em.
我太喜欢了<?=$whatever?>,放不下。从来没有问题。我会等到它咬我的屁股。严肃地说,85% 的(我的)客户在极少数情况下可以访问 php.ini 。其他 15% 使用主流托管服务提供商,并且几乎所有人都启用了它们。我爱他们。
回答by dukeofgaming
Starting with PHP 5.4, the echo shortcut is a separate issue from short tags, as the echo shortcut will always be enabled. It's a fact now:
从 PHP 5.4 开始,回显快捷方式是与短标签不同的问题,因为回显快捷方式将始终启用。现在是事实:
So the echo shortcut itself (<?=) is safe to use now.
所以 echo 快捷方式本身 ( <?=) 现在可以安全使用。
回答by Brian Lacy
The problem with this whole discussion lies in the use of PHP as a templating language. No one is arguing that tags should be used in application source files.
整个讨论的问题在于使用 PHP 作为模板语言。没有人认为应该在应用程序源文件中使用标签。
However PHP's embeddable syntax allows it to be used as a powerful template language, and templates should be as simple and readable as possible. Many have found it easier to use a much slower, add-on templating engine like Smarty, but for those purists among us who demand fast rendering and a pure code base, PHP is the only way to write templates.
然而,PHP 的可嵌入语法允许它作为一种强大的模板语言使用,并且模板应该尽可能简单易读。许多人发现使用像 Smarty 这样慢得多的附加模板引擎更容易,但对于我们当中那些需要快速渲染和纯代码库的纯粹主义者来说,PHP 是编写模板的唯一方法。
The ONLY valid argument AGAINST the use of short tags is that they aren't supported on all servers. Comments about conflicts with XML documents are ludicrous, because you probably shouldn't be mixing PHP and XML anyway; and if you are, you should be using PHP to output strings of text. Security should never be an issue, because if you're putting sensitive information like database access credentials inside of template files, well then, you've got bigger issues!
反对使用短标签的唯一有效参数是,并非所有服务器都支持它们。关于与 XML 文档冲突的评论是荒谬的,因为您可能无论如何都不应该混合 PHP 和 XML;如果是,您应该使用 PHP 来输出文本字符串。安全永远不会成为问题,因为如果您将数据库访问凭据等敏感信息放在模板文件中,那么,您就会遇到更大的问题!
Now then, as to the issue of server support, admittedly one has to be aware of their target platform. If shared hosting is a likely target, then short tags should be avoided. But for many professional developers (such as myself), the client acknowledges (and indeed, depends on the fact) that we will be dictating the server requirements. Often I'm responsible for setting up the server myself.
那么,至于服务器支持的问题,无可否认,人们必须了解他们的目标平台。如果共享主机可能是目标,则应避免使用短标签。但是对于许多专业开发人员(例如我自己),客户承认(实际上,取决于事实)我们将决定服务器要求。通常我负责自己设置服务器。
And we NEVER work with a hosting provider that does not give us absolute control of the server configuration -- in such a case we could count on running to much more trouble than just losing short tag support. It just doesn't happen.
而且我们从不与不给我们绝对控制服务器配置的托管服务提供商合作——在这种情况下,我们可以指望运行会带来更多的麻烦,而不仅仅是失去短标签支持。它只是不会发生。
So yes -- I agree that the use of short tags should be carefully weighed. But I also firmly believe that it should ALWAYS be an option, and that a developer who is aware of his environment should feel free to use them.
所以是的 - 我同意应该仔细权衡短标签的使用。但我也坚信它应该始终是一种选择,并且了解其环境的开发人员应该可以随意使用它们。
回答by Jake McGraw
Short tags are coming back thanks to Zend Frameworkpushing the "PHP as a template language" in their default MVC configuration. I don't see what the debate is about, most of the software you will produce during your lifetime will operate on a server you or your company will control. As long as you keep yourself consistent, there shouldn't be any problems.
由于Zend Framework在其默认 MVC 配置中推动“ PHP 作为模板语言”,短标签又回来了。我不明白争论的内容是什么,您一生中将开发的大多数软件都将在您或您的公司将控制的服务器上运行。只要你保持自己的一致性,就不应该有任何问题。
UPDATE
更新
After doing quite a bit of work with Magento, which uses long form. As a result, I've switched to the long form of:
在使用Magento做了很多工作之后,它使用了长格式。结果,我切换到了长格式:
<?php and <?php echo
over
超过
<? and <?=
Seems like a small amount of work to assure interoperability.
似乎是确保互操作性的少量工作。
回答by Vinko Vrsalovic
Because the confusion it can generate with XML declarations. Many people agreewithyou, though.
An additional concern is the pain it'd generate to code everything with short tags only to find out at the end that the final hosting server has them turned off...
另一个问题是使用短标签对所有内容进行编码会产生痛苦,但最终发现最终托管服务器已将它们关闭......
回答by Sumoanand
Following is the wonderful flow diagram of the same:
以下是相同的精彩流程图:


Source: similiar question on Software Engineering Stack Exchange
回答by Oliver Charlesworth
http://uk3.php.net/manual/en/language.basic-syntax.phpmode.phphas plenty of advice, including:
http://uk3.php.net/manual/en/language.basic-syntax.phpmode.php有很多建议,包括:
while some people find short tags and ASP style tags convenient, they are less portable, and generally not recommended.
虽然有些人觉得短标签和 ASP 样式标签方便,但它们的可移植性较差,一般不推荐使用。
and
和
note that if you are embedding PHP within XML or XHTML you will need to use the
<?php ?>tags to remain compliant with standards.
请注意,如果您将 PHP 嵌入到 XML 或 XHTML 中,您将需要使用
<?php ?>标签来保持符合标准。
and
和
Using short tags should be avoided when developing applications or libraries that are meant for redistribution, or deployment on PHP servers which are not under your control, because short tags may not be supported on the target server. For portable, redistributable code, be sure not to use short tags.
在开发用于重新分发或部署在不受您控制的 PHP 服务器上的应用程序或库时,应避免使用短标签,因为目标服务器可能不支持短标签。对于可移植、可再分发的代码,请确保不要使用短标签。
回答by James Alday
In case anyone's still paying attention to this... As of PHP 5.4.0 Alpha 1 <?=is always available:
如果有人还在关注这个……从 PHP 5.4.0 Alpha 1 开始<?=,始终可用:
http://php.net/releases/NEWS_5_4_0_alpha1.txt
http://php.net/releases/NEWS_5_4_0_alpha1.txt
So it looks like short tags are (a) acceptable and (b) here to stay. For now at least...
所以看起来短标签是 (a) 可以接受和 (b) 留在这里。至少目前...
回答by ConroyP
Short tags are not turned on by default in some webservers (shared hosts, etc.), so code portabilitybecomes an issue if you need to move to one of these.
Readabilitymay be an issue for some. Many developers may find that
<?phpcatches the eye as a more obvious marker of the beginning of a code block than<?when you scan a file, particularly if you're stuck with a code base with HTMLand PHP tightly inter-woven.
在某些网络服务器(共享主机等)中默认情况下不启用短标签,因此如果您需要移动到其中之一,代码可移植性就会成为一个问题。
对于某些人来说,可读性可能是一个问题。许多开发人员可能会发现,
<?php与<?扫描文件时相比,它是代码块开头的更明显标记,特别是当您坚持使用HTML和 PHP 紧密交织的代码库时。

