0

我想知道是否有比在 PHP 中插入文本更短的方法

<?php
$city = "London";
?>
This website is a funky guide to <?php print $city; ?>!!!

例如,在 Rails 上使用 ruby​​,我可以设置

city = 'London'

在代码的某个地方,在我的 .erb 文件中我可以做

This website is a funky guide to <%= city %>!!!

我确实在某个{$city}可以使用的地方阅读过,但我试过了,但没有。那么有比 更短的形式<?php print $var; ?>吗?

4

4 回答 4

5

您可以使用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 代码的文件,必须始终省略结束标记

来源


我希望这些信息是有用的,并为您的评论带来某种答案:-)

于 2009-08-02T14:33:18.420 回答
2

如果您切换出 PHP 模式,那么打印变量的唯一方法是切换回它并像上面那样打印它。您可以保持 PHP 模式并使用您尝试过的 {} 构造:

<?php
$city = "London";
echo "This website is a funky guide to {$city}!!!";
?>
于 2009-08-02T14:32:41.777 回答
1

我确实在某处读过可以使用 {$city}

如果您使用Smarty等模板引擎,您可以这样做。如果您在一个团队中开发并且其中一些人不了解 PHP,这可能会很有用。

否则,假设您启用了短标签,您将获得的最短时间如下:

<?=$city?>

如果你不这样做,或者你正在创建一个应用程序来重新分发给其他人,那么使用这个:

<?php echo $city ?>
于 2009-08-02T15:46:54.840 回答
0

只需扩展您的 PHP 块并执行以下操作:

<?php
$city = 'London';
echo 'This website is a funky guide to ',$city,' !!!';
?>

或者,您可以使用 " " 代替 ' ' 并替换',$city,'$city,但解析变量需要更多 CPU 时间。

于 2009-08-02T14:34:04.783 回答