您可以使用short_open_tag
, 必须在您的配置中启用它,但这不是一个好的做法,因为它仅在启用它们时才有效——而且它们并非总是如此(甚至默认情况下可能不是)
使用长标签和回显/打印可能会更长,是的......但我建议使用这些标签,而不是短标签。
另请注意,当数据来自不受信任的来源和/或可能包含您不想在页面中注入的 HTML 时,您可能需要转义数据,以避免注入 HTML/JS (请参阅htmlspecialchars):
在评论后编辑,添加一些关于short_open_tag
:
为什么短开放标签被认为(至少我是^^)是不好的做法?
首先,经过一些检查,默认情况下它们是不启用的:
对于 PHP 5.3:
squale@shark:~/temp/php/php-5.3.0
$ grep 'short_open_tag' php.ini-development
; short_open_tag
short_open_tag = Off
squale@shark:~/temp/php/php-5.3.0
$ grep 'short_open_tag' php.ini-production
; short_open_tag
short_open_tag = Off
development
在“ ”或“ production
”设置中默认禁用。
对于 PHP 5.2.10 (PHP 5.2 的最新版本):
squale@shark:~/temp/php/php-5.2.10
$ grep 'short_open_tag' php.ini-dist
short_open_tag = On
squale@shark:~/temp/php/php-5.2.10
$ grep 'short_open_tag' php.ini-recommended
; - short_open_tag = Off [Portability]
short_open_tag = Off
在“ recommended
”设置中默认禁用
考虑到这些默认设置有时(通常?)由托管服务保留,依赖于short_open_tag
被激活是很危险的。
(我自己遇到了那些被禁用的问题......当你不是服务器的管理员并且不需要修改它的特权时,这并不好玩^^)
如果你想要一些数字,你可以看一下快速调查:short_open_tag
默认情况下支持打开还是关闭?
(不是科学证明——但表明将这些用于您将向公众发布的应用程序可能是危险的)
就像你说的那样,那些在激活时与 XML 声明冲突——意味着你必须使用这样的东西:
<?php echo '<?xml version="1.0" encoding="UTF-8" ?>'; ?>
考虑到存在短开放标签,并且可能在您将使用的服务器上激活,但您应该可能<?xml
永远不会使用;太糟糕了:-(
实际上,通读 PHP 5.2.10 的 php.ini-recommended :
; Allow the <? tag. Otherwise, only <?php and <script> tags are recognized.
; 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.
PHP 6 中的那个更有趣:
; This directive determines whether or not PHP will recognize code between
; <? and ?> tags as PHP source which should be processed as such. It's been
; recommended for several years that you not use the short tag "short cut" and
; instead to use the full <?php and ?> tag combination. With the wide spread use
; of XML and use of these tags by other languages, the server can become easily
; confused and end up parsing the wrong code in the wrong context. But because
; this short cut has been a feature for such a long time, it's currently still
; supported for backwards compatibility, but we recommend you don't use them.
(在 PHP 5.3 中可能相同;没有检查)
有传言称PHP 6 可以删除短开放标签;考虑到我刚刚发布的 php.ini 部分,它可能不会......但是,仍然......
给出一个指向另一个方向的论点(我必须说实话,毕竟):在 Zend Framework 的使用模板文件的示例中(仅)为模板文件 使用短的打开标记是经常做的事情:
在我们的示例和文档中,我们使用 PHP 短标签:
也就是说,许多开发人员更喜欢使用完整标签来进行验证或可移植性。例如,在 php.ini.recommended 文件中禁用了 short_open_tag,如果您在视图脚本中模板化 XML,短打开标签将导致模板验证失败。
(来源)
相反,对于.php
文件:
绝不允许使用短标签。对于仅包含 PHP 代码的文件,必须始终省略结束标记
(来源)
我希望这些信息是有用的,并为您的评论带来某种答案:-)