是否可以接受PHP短标签来使用? [英] Are PHP short tags acceptable to use?

查看:144
本文介绍了是否可以接受PHP短标签来使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Here's the information according to the official documentation:


有四组不同的
开头和结尾标签,可以是PHP中使用的
。其中两个,<?php?>
< script language =php> < / script>
始终可用。另外两个
是短标签和ASP样式标签,
可以从
php.ini配置文件打开和关闭。因此,
同时有些人发现短标签和
ASP样式标签方便,它们是
少便携式,并且一般不是
。 p>

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

<?=

$ b b

比输入更方便

is far more convenient than typing

<?php echo 

程序员的便利性是一个重要因素,因此为什么不建议这样做?

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

推荐答案

不推荐使用它们,因为它是一个PITA,如果你必须将代码移动到不支持的服务器它)。正如你所说,许多共享主机 支持短标签,但批不是全部。

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

我同意<?如果你想分享脚本,最好使用完整的语法。 <?php < ;? php echo 但是可以做一个批量查找​​和替换,只要你每次使用相同的形式(并且不吸引空格(例如:< ;?php < ;? =

I agree that <? and <?= are easier on programmers than <?php and <?php echo but 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: <? php or <? =)

正如ThiefMaster在评论中所提到的, PHP 5.4,<?= ...?> 标签随处可见,无论是否为短标签设置 。这意味着它们可以安全地在可移植代码中使用,但这意味着对PHP 5.4+的依赖,如果你想要支持5.4之前的版本并且不能保证短标签,您仍然需要使用<?php echo ...?>

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

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

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.

这篇关于是否可以接受PHP短标签来使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆