PHP 开放标签“<?="和“<?php"/“<?"之间有什么区别?

2021-12-29 00:00:00 syntax php

抱歉这个愚蠢的问题,但我遇到了使用的代码:

printOnLoad();?><?=$MAP_OBJECT->printMap();?><?=$MAP_OBJECT->printSidebar();?>

<?= 相比有什么特别之处,或者只是简单的 <??

解决方案

而不是谈论是否short_open_tags 是否已弃用我们应该谈谈使用短开放标签时的优缺点:

优势

使用短的开始标签 <?<?= 比标准的开始标签 <?php 更短并且可能更容易编写 分别.在模板中直接使用 PHP 时,这非常方便.(这可能也是 PHP 具有控制结构的替代语法的原因.)>

缺点

需要特定的配置

使用短开放标签时,您需要启用short_open_tags.如果您或您的网络托管服务提供商决定禁用 short_open_tags,您的应用程序可能无法再运行,并且您可能会遇到一些严重的安全问题.因为如果 short_open_tags 被禁用,只有标准的开始标签 被识别,并且短开始标签内的所有内容都被视为纯文本.(另请参阅 Sarfraz Ahmed 引用的博客文章回答.)

如果您的目标是编写不适合您的应用程序,则此要求会降低您的 PHP 应用程序的可移植性.这也是为什么许多人建议不要使用短的开放标签(包括 PHP 手册)的原因:

<块引用>

注意:在开发用于重新分发或部署在不受您控制的 PHP 服务器上的应用程序或库时,应避免使用短标签,因为短标签可能不受支持目标服务器.对于可移植、可再分发的代码,请确保不要使用短标签.

从 PHP 5.4 开始, 始终可用,无论 short_open_tags 选项如何. 另一方面,需要启用该选项.

与 XML 处理指令冲突

另一个问题是在使用 XML 处理指令(例如 <)时;?xml ... ?>.当启用 short_open_tags 时,您不能直接在代码中使用它们,而需要使用 PHP 来输出它:

<块引用>

如果您想将 PHP 与 XML 结合使用,您可以禁用此选项以使用 内联.否则,可以用PHP打印,例如: ';?>.

否则 PHP 会阻塞在 中的 xml.

关于弃用的最后一句话:目前 short_open_tags 未弃用.否则手册会明确说明这一点.此外,PHP 的发明者 Rasmus Lerdorf 在回复中写道问题short_open_tag 在 PHP 6 中被弃用是真的吗?"在内部邮件列表上,有几个原因不删除 PHP 6 中的short_open_tags:

<块引用>

这是我们决定不在 PHP 6 中删除它们的原因之一.

Sorry for the silly question, but I ran across code that used:

<?=$MAP_OBJECT->printOnLoad();?>
<?=$MAP_OBJECT->printMap();?>
<?=$MAP_OBJECT->printSidebar();?>

Is there anything special about <?= over <?php or just plain <??

解决方案

Rather than talking about whether short_open_tags is deprecated or not we should talk about the advantages and disadvantages when using short open tags:

Advantages

Using the short open tags <? along with <?= is shorter and probably easier to write than the standard opening tags <?php and <?php echo respectively. That’s quite handy when using PHP directly in a template. (That’s probably also the reason why PHP has an alternative syntax for control structures.)

Disadvantages

Requires a specific configuration

When using short open tags you are required to have short_open_tags enabled. If you or your web hosting provider decides to disable short_open_tags, your application probably won’t work any more and you can have some serious security issues. Because if short_open_tags is disabled, only the standard opening tags <?php are recognized and everything inside short opening tags is treated as plain text. (See also the blog post referenced in Sarfraz Ahmed’s answer.)

This requirement makes your PHP application less portable if you aim to write applications that are not just for you. That’a also why many recommend not to use short open tags (including the PHP manual):

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

As of PHP 5.4 <?= is always available, regardless of the short_open_tags option. <? on the other hand requires the option to be enabled.

Conflicts with XML processing instructions

Another issue is when using XML processing instructions like <?xml … ?>. When short_open_tags is enabled you cannot use them directly in your code but need to use PHP to output it:

If you want to use PHP in combination with XML, you can disable this option in order to use <?xml ?> inline. Otherwise, you can print it with PHP, for example: <?php echo '<?xml version="1.0"?>'; ?>.

Otherwise PHP will choke on the xml in <?xml.

Now some last words about the deprecation: Currently short_open_tags is not deprecated. Otherwise the manual would state that explicitly. Additionally, Rasmus Lerdorf, inventor of PHP, wrote in a reply on the question "Is it true that short_open_tag is deprecated in PHP 6?" on the internals mailing list that there were several reasons not to remove short_open_tags in PHP 6:

Which is one of the reasons we decided not to remove them in PHP 6.

相关文章