PHP echo 与 PHP 短标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1386620/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:20:20  来源:igfitidea点击:

PHP echo vs PHP short tags

phpfunctionechopageload

提问by homework

Are they equal in safeness? I was informed that using

他们的安全性是否相等?我被告知使用

<?=$function_here?>

was less safe, and that it slows down page load times. I am strictly biased to using echo.

不太安全,并且会减慢页面加载时间。我严格偏向于使用 echo。

What are the advantages/disadvantages?

有什么优点/缺点?

回答by Pascal MARTIN

<?and <?=are called short open tags, and are not always enabled (see the short_open_tagdirective) with PHP 5.3 or below (but since PHP 5.4.0, <?=is always available).

<?并且<?=被称为短开放标签,并且在short_open_tagPHP 5.3 或更低版本中并不总是启用(请参阅指令)(但自 PHP 5.4.0 以来,<?=始终可用)。

Actually, in the php.ini-production file provided with PHP 5.3.0, they are disabled by default:

实际上,在 PHP 5.3.0 提供的 php.ini-production 文件中,它们默认是禁用的:

$ grep 'short_open' php.ini-production
; short_open_tag
short_open_tag = Off

So, using them in an application you want to distribute might not be a good idea: your application will not work if they are not enabled.

因此,在您要分发的应用程序中使用它们可能不是一个好主意:如果未启用它们,您的应用程序将无法运行。

<?php, on the other side, cannot be disabled -- so, it's safest to use this one, even if it is longer to write.

<?php,另一方面,不能被禁用——所以,使用这个是最安全的,即使它写的时间更长。


Except the fact that short open tags are not necessarily enabled, I don't think there is much of a difference.


除了不一定启用短开放标签这一事实外,我认为没有太大区别。

回答by carl

Echo is generally just better to use because...

Echo 通常更好用,因为...

  1. It supports good programming style.
  2. It can't be turned off in php.ini (short tags can be)
  3. Short tags will be removed in PHP 6)
  1. 它支持良好的编程风格。
  2. php.ini中不能关闭(短标签可以)
  3. 短标签将在 PHP 6 中删除)

But, they are generally the same. See also:

但是,它们通常是相同的。也可以看看:

  1. Are PHP short tags acceptable to use?
  2. How are echo and print different in PHP?
  1. 可以使用 PHP 短标签吗?
  2. PHP 中的 echo 和 print 有何不同?

回答by Frank

Apart from the whole semi-religious debate on whether or not using short tags are a good idea and whether or not it should be considered deprecated, the original question was on how safe or unsafe they are to use.

除了关于使用短标签是否是一个好主意以及是否应该被弃用的整个半宗教辩论之外,最初的问题是使用它们的安全性或不安全性。

Simply put, if you use short tags on a server that doesn't support them, parts of your PHP code may be exposed which can be considered a security vulnerability.

简而言之,如果您在不支持短标签的服务器上使用短标签,您的部分 PHP 代码可能会暴露,这可以被视为安全漏洞。

回答by Blouarf

http://php.net/manual/en/language.basic-syntax.phpmode.phpstates:

http://php.net/manual/en/language.basic-syntax.phpmode.php指出:

Starting with PHP 5.4, short echo tag is always recognized and valid, regardless of the short_open_tagsetting.

从 PHP 5.4 开始,无论short_open_tag设置如何,短回显标签始终被识别和有效。

short_open_tagOff or On doesn't matter anymore.

short_open_tag关或开不再重要。

So now you can, without concern, put tags like this in your templates:

所以现在您可以毫无顾虑地在模板中放置这样的标签:

    <?= (($test) ? "val1" : "val2") ?>

It is official now, the "short echo tag" is something very different than the "short tag".

现在是官方的,“短回声标签”与“短标签”非常不同。

回答by leopold

Just to ad another source of PSR: http://www.php-fig.org/psr/psr-1/

只是为了广告 PSR 的另一个来源:http: //www.php-fig.org/psr/psr-1/

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

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

specifying:

指定:

 <?php ?> and <?= ?>

回答by Ritey

So why don't they just remove the option to turn off short open tag and let it be enabled by default.

那么为什么他们不删除关闭短开标签的选项并让它在默认情况下启用。

This is a VERY dangerous move by PHP. The reason being, if you put your existing code that has short tags in it on a PHP6 server and someone views that page, they will get the raw code downloaded to their browser which you can view. This could seriously kill off PHP.

这是 PHP 非常危险的举动。原因是,如果您将包含短标签的现有代码放在 PHP6 服务器上并且有人查看该页面,他们会将原始代码下载到您可以查看的浏览器。这可能会严重杀死 PHP。

回答by Lucas

You should use <\?= and ask your sysadmin/host to turn the short_open_tags on, it doesn't have any cons to be turned off, also it has no slowdown, the parser look for the <\? the same way it look for <\?php inside the index, binnary search (perhaps

您应该使用 <\?= 并要求您的系统管理员/主机打开 short_open_tags,它没有任何要关闭的缺点,也没有减速,解析器会查找 <\? 与它在索引中查找 <\?php 的方式相同,二进制搜索(也许

Also, already on PHP 5.5 <\?= will be turned on by default and on long term <\?= will be prefered while <\?php echo semi deprecated.

此外,已经在 PHP 5.5 上 <\?= 将默认打开,并且长期 <\?= 将被首选,而 <\?php echo semi 已弃用。

If not surte yet.. A bit of googling helps a lot =D

如果还没有 surte .. 一些谷歌搜索有很大帮助 =D