PHP 短标签可以使用吗?

发布于 2024-07-07 09:29:02 字数 622 浏览 8 评论 0 原文

以下是根据官方文档的信息:

有四对不同的 可以是开始和结束标签 在 PHP 中使用。 其中两个,<脚本语言=“php”> , 随时可用。 另外两个 是短标签和 ASP 样式标签,并且 可以从打开和关闭 php.ini 配置文件。 像这样, 虽然有些人发现短标签并且 ASP风格的标签方便,它们是 不太便携,并且通常不 推荐

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

<?=

远比打字方便

<?php echo 

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

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
.

In my experience most servers do have short tags enabled. Typing

<?=

is far more convenient than typing

<?php echo 

The programmers convenience is an important factor, so why are they not recommended?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(28

青瓷清茶倾城歌 2024-07-14 09:29:02

PHP 短标记 () 和简写 echo 标记 () 之间必须有明确的区别,

前者是 PHP 编码标准,主要是出于常识,因为如果你必须移动你的程序,它就是一个 PITA将代码发送到不支持它的服务器(并且您无法启用它)。 正如您所说,许多共享主机确实支持短标签,但“很多”并不是全部。 如果您想共享脚本,最好使用完整语法。

而速记 echo 标签 无法禁用,因此完全可以使用。

我同意 对程序员来说比 更容易,但只要使用相同的表单就可以进行批量查找和替换每一次。

我根本不把可读性作为理由。 大多数认真的开发人员都可以选择语法突出显示。

正如 ThiefMaster 在评论中提到的,从 PHP 5.4 开始,< ;?= ... ?> 标签在任何地方都受支持,无论短标签设置如何。 这应该意味着它们可以安全地在可移植代码中使用,但这确实意味着对 PHP 5.4+ 的依赖。 如果您想支持 5.4 之前的版本并且不能保证短标签,您仍然需要使用

另外,您需要知道 ASP 标签 <% ,%> 、 <%= 和 script 标签已从 PHP 7 中删除。 因此,如果您想支持长期可移植代码并希望切换到最现代的工具,请考虑更改这部分代码。

There must be a clear distinction between the PHP short tag (<?) and shorthand echo tag (<?=)

The former is prohibited by the PHP Coding standard, mostly out of common sense 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 do support shorttags but "lots" isn't all of them. If you want to share your scripts, it's best to use the full syntax.

Whereas the shorthand echo tag <?= cannot be disabled and therefore is fully acceptable to use.

I agree that <? is easier on programmers than <?php but it is possible to do a bulk find-and-replace as long as you use the same form each time.

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 ... ?>.

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.

远昼 2024-07-14 09:29:02

我太喜欢而无法放手。 从来没有遇到过问题。 我会等到它咬我屁股的时候。 严肃地说,85% 的(我的)客户在罕见关闭的情况下仍可以访问 php.ini。 另外 15% 使用主流托管提供商,而且几乎所有提供商都启用了这些服务。 我爱他们。

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 rare occasion they are turned off. The other 15% use mainstream hosting providers, and virtually all of them have them enabled. I love 'em.

南汐寒笙箫 2024-07-14 09:29:02

从 PHP 5.4 开始,echo 快捷方式与短标签是一个单独的问题,因为 echo 快捷方式将始终启用。 现在已经成为事实:

所以 echo 快捷方式本身 () 现在可以安全使用。

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:

So the echo shortcut itself (<?=) is safe to use now.

海拔太高太耀眼 2024-07-14 09:29:02

整个讨论的问题在于使用 PHP 作为模板语言。 没有人争论应该在应用程序源文件中使用标签。

然而,PHP 的可嵌入语法使其可以用作强大的模板语言,并且模板应尽可能简单且可读。 许多人发现使用像 Smarty 这样速度慢得多的附加模板引擎会更容易,但对于我们当中那些需要快速渲染和纯代码库的纯粹主义者来说,PHP 是编写模板的唯一方法。

反对使用短标签的唯一有效论点是,并非所有服务器都支持它们。 关于与 XML 文档冲突的评论是可笑的,因为无论如何您可能不应该混合 PHP 和 XML; 如果是的话,您应该使用 PHP 来输出文本字符串。 安全永远不应该成为问题,因为如果您将数据库访问凭据等敏感信息放入模板文件中,那么,您就会遇到更大的问题!

那么,说到服务器支持问题,诚然,人们必须了解他们的目标平台。 如果共享托管是可能的目标,则应避免使用短标签。 但对于许多专业开发人员(例如我自己)来说,客户承认(实际上,取决于事实)我们将规定服务器要求。 通常我负责自己设置服务器。

而且我们永远不会与不能让我们绝对控制服务器配置的托管提供商合作——在这种情况下,我们可能会遇到比仅仅失去短标签支持更多的麻烦。 它就是不会发生。

所以是的——我同意应该仔细权衡短标签的使用。 但我也坚信它应该始终是一种选择,并且了解其环境的开发人员应该随意使用它们。

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.

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.

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!

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.

枯寂 2024-07-14 09:29:02

由于 Zend Framework 推动“PHP 作为模板语言”在他们的 默认 MVC 配置。 我不明白争论的焦点是什么,您一生中将生产的大多数软件都将在您或您的公司控制的服务器上运行。 只要你保持一致,就不会有任何问题。

更新

在使用Magento(使用长格式)进行了大量工作之后。 因此,我改用了长形式:

<?php and <?php echo

over

<? and <?=

似乎只需少量工作即可确保互操作性。

Short tags are coming back thanks to Zend Framework pushing 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.

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:

<?php and <?php echo

over

<? and <?=

Seems like a small amount of work to assure interoperability.

她比我温柔 2024-07-14 09:29:02

因为它可能会与 XML 声明产生混淆。 许多人同意 你,不过。

另一个问题是,用短标签对所有内容进行编码会产生痛苦,最后却发现最终托管服务器已将它们关闭......

Because the confusion it can generate with XML declarations. Many people agree with you, 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...

段念尘 2024-07-14 09:29:02

下面是同样精彩的流程图:

决策树的使用 <?=

来源:有关软件工程堆栈交换的类似问题

Following is the wonderful flow diagram of the same:

decision making tree of the use of <?=

Source: similiar question on Software Engineering Stack Exchange

国产ˉ祖宗 2024-07-14 09:29:02

如果有人仍然关注这一点...从 PHP 5.4.0 Alpha 1 始终可用:

http://php.net/releases/NEWS_5_4_0_alpha1.txt

所以看起来短标签是 (a) 可以接受的并且 ( b) 留在这里。 至少目前...

In case anyone's still paying attention to this... As of PHP 5.4.0 Alpha 1 <?= is always available:

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...

与酒说心事 2024-07-14 09:29:02

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

虽然有些人发现短标签并且
ASP风格的标签方便,它们是
不太便携,而且一般不
推荐。

请注意,如果您嵌入 PHP
在 XML 或 XHTML 中,您需要
使用 标签保留
符合标准。

应避免使用短标签
当开发应用程序或
旨在用于
重新分发或在 PHP 上部署
不属于您的服务器
控制,因为短标签可能不
目标服务器上支持。 为了
可移植的、可重新分发的代码
确保不要使用短标签。

https://www.php.net/manual/en/language.basic-syntax.phpmode.php has plenty of advice, including:

while some people find short tags and
ASP style tags convenient, they are
less portable, and generally not
recommended.

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.

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.

人间☆小暴躁 2024-07-14 09:29:02
  • 默认情况下,某些网络服务器(共享主机等)中不会启用短标记,因此,如果您需要迁移到其中之一,代码可移植性就会成为问题。

  • 可读性对某些人来说可能是个问题。 许多开发人员可能会发现,在扫描文件时, 作为代码块开头的标记比 更引人注目,尤其是在扫描文件时你被困在 HTML 和 PHP 紧密交织的代码库中。

  • Short tags are not turned on by default in some webservers (shared hosts, etc.), so code portability becomes an issue if you need to move to one of these.

  • Readability may be an issue for some. Many developers may find that <?php catches 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 HTML and PHP tightly inter-woven.

郁金香雨 2024-07-14 09:29:02

注意:从 PHP 5.4 开始,短标记 现在始终可用。

Note: Starting in PHP 5.4 the short tag, <?=, is now always available.

蝶…霜飞 2024-07-14 09:29:02

我在查找有关该主题的信息后阅读了此页面,我觉得没有提到一个主要问题:懒惰与一致性。 PHP 的“真正”标签是。 为什么? 我真的不在乎。 当那些明显适用于 PHP 的东西时,为什么要使用其他东西呢? <%和%> 对我来说意味着 ASP,而

另一方面,我同意模板中的短标签(并且仅在模板中)似乎很有用,但问题是我们刚刚花了太多时间在这里讨论它,可能需要很长时间才能真正浪费花了这么多时间输入额外的三个字符“php”!

虽然有很多选择固然很好,但这一点也不符合逻辑,而且可能会导致问题。 想象一下,如果每种编程语言都允许 4 种或更多类型的标签:Javascript 可以是

最后,我不认为短标签是这里的问题:PHP 代码块只有两种逻辑类型—— 1) 常规 PHP 代码,2) 模板回显。
对于前者,我坚信只有 才可以。 应该允许只是为了保持一切一致和可移植。
对于后者, 方法很丑陋。 为什么一定要这样呢? 为什么不添加一些更符合逻辑的东西呢?

这不会做任何事情(并且只有在最遥远的可能性中它才会与某些东西发生冲突),并且可以轻松地取代笨拙的 相反,不用担心不一致。

当打开和关闭标签有 4 个选项以及随机添加特殊的“echo”标签时,PHP 也可能在 php.ini 或 .htaccess 中有一个“自定义打开/关闭标签”标志。 这样设计师就可以选择他们最喜欢的一个。 但出于显而易见的原因,这有点矫枉过正了。 那么为什么要允许 4 个以上的选项呢?

I read this page after looking for information on the topic, and I feel that one major issue has not been mentioned: laziness vs. consistency. The "real" tags for PHP are <?php and ?>. Why? I don't really care. Why would you want to use something else when those are clearly for PHP? <% and %> mean ASP to me, and <script ..... means Javascript (in most cases). So for consistency, fast learning, portability, and simplicity, why not stick to the standard?

On the other hand I agree that short tags in templates (and ONLY in templates) seem useful, but the problem is that we've just spent so much time discussing it here, that it would likely take a very long time to have actually wasted that much time typing the extra three characters of "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.

At the point where there are 4 options for open and close tags and the random addition of a special "echo" tag, PHP may as well have a "custom open/close tags" flag in php.ini or .htaccess. That way designers can choose the one they like best. But for obvious reasons that's overkill. So why allow 4+ options?

格子衫的從容 2024-07-14 09:29:02

截至 2019 年,我不同意这里的某些答案。 推荐使用:

1. 长标签

<?php /* code goes here */ ?>

2. 短 echo 标签

<?= /* code goes here */ ?>

原因:PSR-1 基本编码推荐使用它们标准

其他短标签,例如 不推荐。

规范说:

PHP 代码必须使用长标签或短回显
标签; 它不得使用其他标签变体

As of 2019 I disagree with certain answers here. Recommended to use:

1. Long tags

<?php /* code goes here */ ?>

2. Short echo tags

<?= /* code goes here */ ?>

Reason: They are recommended by the PSR-1 basic coding standard

Other short tags like <? /* code goes here */ ?> are not recommended.

The spec says:

PHP code MUST use the long tags or the short-echo
tags; it MUST NOT use the other tag variations.

八巷 2024-07-14 09:29:02

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

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

One situation that is a little different is when developing a CodeIgniter application. CodeIgniter seems to use the shorttags whenever PHP is being used in a template/view, otherwise with models and controllers it always uses the long tags. It's not a hard and fast rule in the framework, but for the most part the framework and a lot of the source from other uses follows this convention.

My two cents? If you never plan on running the code somewhere else, then use them if you want. I'd rather not have to do a massive search and replace when I realize it was a dumb idea.

与君绝 2024-07-14 09:29:02

在较新版本中, 默认情况下处于禁用状态。 您可以按照描述启用此功能 启用 Short PHP 中的标签

<? is disabled by default in newer versions. You can enable this like described Enabling Short Tags in PHP.

无敌元气妹 2024-07-14 09:29:02

我认为值得一提的是,从 PHP 7 开始:

  • 短 ASP PHP 标签 <% … %> 消失了
  • 短 PHP 标签 short_open_tag 设置为 true,... ?> 仍然可用。 这是默认设置。
  • 从 PHP 5.4 开始,无论 short_open_tagPrint 标签始终启用> 设置。

很好地摆脱了第一个,因为它干扰了其他语言。

除了个人喜好之外,现在没有理由不使用短打印标签。

当然,如果您编写的代码与 PHP 5 的旧版本兼容,则需要遵守旧规则,但请记住,PHP 5.6 之前的任何内容现在均不受支持。

请参阅:https://secure.php.net/manual/ en/language.basic-syntax.phptags.php

另请注意,上述参考不鼓励使用第二个版本 (),因为它可能已被禁用:

注意:
由于可以禁用短标签,因此建议仅使用普通标签()以最大限度地提高兼容性。

I thought it worth mentioning that as of PHP 7:

  • Short ASP PHP tags <% … %> are gone
  • Short PHP tabs <? … ?> are still available if short_open_tag is set to true. This is the default.
  • Since PHP 5.4, Short Print tags <?=… ?> are always enabled, regardless of the short_open_tag setting.

Good riddance to the first one, as it interfered with other languages.

There is now no reason not to use the short print tags, apart from personal preference.

Of course, if you’re writing code to be compatible with legacy versions of PHP 5, you will need to stick to the old rules, but remember that anything before PHP 5.6 is now unsupported.

See: https://secure.php.net/manual/en/language.basic-syntax.phptags.php

Note also the the above reference discourages the second version (<? … ?>) since it may have been disabled:

Note:
As short tags can be disabled it is recommended to only use the normal tags (<?php ?> and <?= ?>) to maximise compatibility.

晨曦÷微暖 2024-07-14 09:29:02

恕我直言,使用短标签的人经常忘记转义他们所回应的内容。 如果有一个默认转义的模板引擎就好了。 我相信 Rob A 编写了一个快速 hack 来转义 Zend Frameworks 应用程序中的短标签。 如果您喜欢短标签,因为它使 PHP 更易于阅读。 那么 Smarty 是否是更好的选择呢?

{$myString|escape}

对我来说这看起来比

<?= htmlspecialchars($myString) ?> 

IMHO people who use short tags often forget to escape whatever they're echoing. It would be nice to have a template engine that escapes by default. I believe Rob A wrote a quick hack to escape short tags in Zend Frameworks apps. If you like short tags because it makes PHP easier to read. Then might Smarty be a better option?

{$myString|escape}

to me that looks better than

<?= htmlspecialchars($myString) ?> 
冰火雁神 2024-07-14 09:29:02

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

It's good to use them when you work with a MVC framework or CMS that have separate view files.
It's fast, less code, not confusing for the designers. Just make sure your server configuration allows using them.

掩耳倾听 2024-07-14 09:29:02

人们不得不问使用短标签的意义是什么。

打字速度更快

MDCore 说:

比输入 方便得多


确实如此。 您无需在整个脚本中输入 X 次 7 个字符。

然而,当一个脚本需要一个小时、10 个小时或更长时间来设计、开发和编写时,在脚本持续时间内不到处输入这 7 个字符的几秒钟时间有多大意义?

与如果短标签未打开或已打开但更新或有人更改 ini 文件/服务器配置导致脚本停止工作而导致某些核心或全部脚本无法工作的可能性相比,还有其他可能性。

您获得的微小好处并不足以抵消潜在问题的严重性,即您的网站无法正常工作,或者更糟糕的是,只有部分无法正常工作,因此需要解决。

更容易阅读

这取决于熟悉程度
我一直看到并使用 。 因此,虽然 并不难读,但它对我来说并不熟悉,因此不太容易阅读

随着前端/后端开发人员的分裂(与大多数公司一样),处理这些模板的前端开发人员会更熟悉知道 等于“ PHP 打开标签并回显"?
我想说,大多数人都会更喜欢更符合逻辑的说法。 也就是说,一个清晰的 PHP 开放标记,然后发生了“echo” -

风险评估
问题 = 整个站点或核心脚本无法工作;

问题的可能性非常低 + 结果的严重性非常高 = 高风险

结论

您节省了一些在这里和那里几秒钟不必输入几个字符,但这样做的风险很大,并且也可能因此失去可读性。

熟悉 的前端或后端编码人员更有可能理解 ,因为它们是标准的 PHP 东西- 标准 开放标签和众所周知的“echo”。
(即使是前端编码人员也应该知道“echo”,否则他们根本不会处理框架提供的任何代码)。

尽管相反的情况不太可能发生,但有人不太可能从逻辑上推断出 PHP 短标记上的等号是“echo”。

One has to ask what the point of using short tags is.

Quicker to type

MDCore said:

<?= is far more convenient than typing <?php echo

Yes, it is. You save having to type 7 characters * X times throughout your scripts.

However, when a script takes an hour, or 10 hours, or more, to design, develop, and write, how relevant is the few seconds of time not typing those 7 chars here and there for the duration of the script?

Compared to the potential for some core, or all, of you scripts not working if short tags are not turned on, or are on but an update or someone changing the ini file/server config stops them working, other potentials.

The small benefit you gain doesn't comes close to outweighing the severity of the potential problems, that is your site not working, or worse, only parts of it not working and thus a headache to resolve.

Easier to read

This depends on familiarity.
I've always seen and used <?php echo. So while <?= is not hard to read, it's not familiar to me and thus not easier to read.

And with front end/back end developer split (as with most companies) would a front end developer working on those templates be more familiar knowing <?= is equal to "PHP open tag and echo"?
I would say most would be more comfortable with the more logical one. That is, a clear PHP open tag and then what is happening "echo" - <?php echo.

Risk assessment
Issue = entire site or core scripts fail to work;

The potential of issue is very low + severity of outcome is very high = high risk

Conclusion

You save a few seconds here and there not having to type a few chars, but risk a lot for it, and also likely lose readability as a result.

Front or back end coders familiar with <?= are more likely to understand <?php echo, as they're standard PHP things - standard <?php open tag and very well known "echo".
(Even front end coders should know "echo" or they simply wont be working on any code served by a framework).

Whereas the reverse is not as likely, someone is not likely to logically deduce that the equals sign on a PHP short tag is "echo".

じ违心 2024-07-14 09:29:02

为了避免可移植性问题,请以 开头 PHP 标签,如果您的 PHP 文件是纯 PHP,没有 HTML,则不需要使用结束标签。

To avoid portability issues, start PHP tags with <?php and in case your PHP file is purely PHP, no HTML, you don't need to use the closing tags.

饮惑 2024-07-14 09:29:02
  • 如果您确定服务器将支持它并且您的开发人员会理解它,则可以使用短标签。
  • 很多服务器不支持,很多开发者看了一遍就明白了。
  • 我使用完整的标签来确保可移植性,因为它确实没有那么糟糕。

话虽如此,我的一个朋友这么说,支持替代标准化 asp 样式标签,例如 <% 而不是 ,这是 php.ini 中名为 asp_tags 的设置。 这是他的推理:

... 任意约定应该是
标准化。 也就是说,任何时候我们
面临着一系列的可能性
都是同等价值的——比如什么
我们的编程中奇怪的标点符号
应该用语言来划分
本身 - 我们应该选择一个标准
并坚持下去。 这样我们
减少所有人的学习曲线
语言(或者任何东西)
公约涉及)。


对我来说听起来不错,但我认为我们中没有人可以围绕这个事业开展工作。 与此同时,我会坚持使用完整的

  • Short tags are acceptable to use in cases where you are certain the server will support it and that your developers will understand it.
  • Many servers do not support it, and many developers will understand it after seeing it once.
  • I use full tags to ensure portability, since it's really not that bad.

With that said, a friend of mine said this, in support of alternate standardized asp-style tags, like <% rather than <?, which is a setting in php.ini called asp_tags. Here is his reasoning:

... arbitrary conventions should be
standardized
. That is, any time we are
faced with a set of possibilities that
are all of equal value - such as what
weird punctuation our programming
language should use to demarcate
itself - we should pick one standard
way and stick with it. That way we
reduce the learning curve of all
languages (or whatever the things the
convention pertains to).

Sounds good to me, but I don't think any of us can circle the wagons around this cause. In the meantime, I would stick to the full <?php.

戴着白色围巾的女孩 2024-07-14 09:29:02

面对现实吧。 如果没有短标签,PHP 就很难看。

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

php_flag short_open_tag on

Let's face it. PHP is ugly as hell without short tags.

You can enable them in a .htaccess file if you can't get to the php.ini:

php_flag short_open_tag on
零度℉ 2024-07-14 09:29:02

如果您关心 XSS 那么您应该使用 大多数时候,所以短标签不会产生很大的区别。

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

我使用 模板引擎,默认情况下它是安全的,并为我编写 标签。

If you care about XSS then you should use <?= htmlspecialchars(…) ?> most of the time, so a short tag doesn't make a big difference.

Even if you shorten echo htmlspecialchars() to h(), it's still a problem that you have to remember to add it almost every time (and trying to keep track which data is pre-escaped, which is unescaped-but-harmless only makes mistakes more likely).

I use a templating engine that is secure by default and writes <?php tags for me.

热风软妹 2024-07-14 09:29:02

短标签在 php 中始终可用。
因此,您不需要 echo 脚本

示例中的第一条语句:

    $a =10;
    <?= $a;//10 
    echo "Hellow";//
    echo "Hellow";

   ?>

突然您需要使用单个 php 脚本,然后您可以
用它。
例子:

<html>
<head>
<title></title>
</head>  
<body>
<p>hellow everybody<?= hi;?></p>
<p>hellow everybody  </p> 
<p>hellow everybody  </p>   
</body>
</html>

Short tag are alwayes available in php.
So you do not need echo the first statement in your script

example:

    $a =10;
    <?= $a;//10 
    echo "Hellow";//
    echo "Hellow";

   ?>

Suddenly you need to use for a single php script then u can
use it.
example:

<html>
<head>
<title></title>
</head>  
<body>
<p>hellow everybody<?= hi;?></p>
<p>hellow everybody  </p> 
<p>hellow everybody  </p>   
</body>
</html>
一百个冬季 2024-07-14 09:29:02

php 中有 3 个标签可用:

  1. 长格式标签 无需指示任何已​​配置的
  2. Short_open_tag 如果有short_open_tag选项则可用
    php.ini 位于
  3. 缩短标签 上,从 php 5.4.0 开始,它始终可用,

从 php 7.0.0 开始,asp 和 script 标签已被删除

3 tags are available in php:

  1. long-form tag that <?php ?> no need to directive any configured
  2. short_open_tag that <? ?> available if short_open_tag option in
    php.ini is on
  3. shorten tag <?= since php 5.4.0 it is always available

from php 7.0.0 asp and script tag are removed

南笙 2024-07-14 09:29:02

> 使用起来要好得多,因为这种编程语言的开发人员已经大规模更新了他们的核心语言。 您可以看到短标签和长标签之间的区别。

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

但是,回显某些内容,例如: 就可以了。 但更喜欢更长的标签。

<?php ?> are much better to use since developers of this programming language has massively updated their core-language. You can see the difference between the short tags and long tags.

Short tags will be highlighted as light red while the longer ones are highlighted darker!

However, echoing something out, for example: <?=$variable;?> is fine. But prefer the longer tags. <?php echo $variable;?>

吃颗糖壮壮胆 2024-07-14 09:29:02

(不带尾随空格)转换为 (带尾随空格):

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

转换 (带 尾随空格)尾随空格)到 (保留尾随空格):

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

Convert <? (without a trailing space) to <?php (with a trailing space):

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

Convert <? (with a trailing space) to <?php (retaining the trailing space):

find . -name "*.php" -print0 | xargs -0 perl -pi -e 's/<\? /<\?php /g'
北音执念 2024-07-14 09:29:02

不,它们正在被逐步淘汰由 PHP 6 编写,因此如果您重视代码的使用寿命,请不要使用它们或 <% ... %> 标记。

No, and they're being phased out by PHP 6 so if you appreciate code longevity, simply don't use them or the <% ... %> tags.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文