简体   繁体   English

PHP模板变量

[英]PHP Template variables <?=

I have some php variables that I need to spit out in a template file. 我有一些需要在模板文件中吐出的php变量。

Normally I would do <?php echo $var; ?> 通常我会做<?php echo $var; ?> <?php echo $var; ?> But I know you can also do <?=$var?> and it will do the same thing. <?php echo $var; ?>但我知道您也可以执行<?=$var?> ,它将执行相同的操作。

I know I've seen a setting for "enable php shorttags" or something like that. 我知道我已经看到了“启用php短标签”或类似的设置。 Meaning that the server will interpret both <? 这意味着服务器将同时解释两个<? and <?php <?php

My question is, do shorttags have to be allowed in order to the method of getting vars <?=$var?> ? 我的问题是,为了获得vars <?=$var?>的方法,是否必须允许使用短标记?

Thanks! 谢谢!

It depends on the PHP version. 它取决于PHP版本。

As of 5.4.0, <?= always works. 从5.4.0开始, <?=始终有效。 Prior to that, short_open_tag needs to be enabled in PHP.ini. 在此之前,需要在PHP.ini中启用short_open_tag

See also: http://php.net/manual/en/ini.core.php 另请参阅: http : //php.net/manual/zh/ini.core.php

I'd avoid using them, for maximum portability. 我会避免使用它们,以实现最大的可移植性。

Yes, short tags is required for <?= . 是的, <?=需要短标签。 People hate on short tags regularly, under the mantra of "your code won't be portable. the new server might not have them enabled!". 人们经常讨厌短标签,这是“您的代码将不可移植。新服务器可能未启用它们!”的口号。 So... if you're going for portability, avoid short tags. 所以...如果您要携带方便,请避免使用短标签。 If you can guarantee the operating environment and can turn them on, then feel free to use them. 如果您可以保证操作环境并可以将其打开,请随时使用它们。

Since PHP 5.4.0, <?= is always available. 从PHP 5.4.0开始, <?=始终可用。 Before that, short_open_tag have to be allowed to using this. 在此之前,必须允许short_open_tag使用它。

The answer is yes for versions of PHP prior to 5.4.0: 对于5.4.0之前的PHP版本,答案是肯定的:

Note: This directive also affected the shorthand <?= before PHP 5.4.0, which is identical to <? 注意:该指令还影响PHP 5.4.0之前的速写<?= ,与<?相同<? echo. 回声。 Use of this shortcut required short_open_tag to be on. 要使用此快捷方式,必须启用short_open_tag。 Since PHP 5.4.0, <?= is always available. 从PHP 5.4.0开始, <?=始终可用。

From the manual: 从手册中:

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. 在开发用于重新分发的应用程序或库或在不受您控制的PHP服务器上进行部署时,应避免使用短标签,因为目标服务器可能不支持短标签。 For portable, redistributable code, be sure not to use short tags. 对于可移植的可再发行代码,请确保不要使用短标签。

Also, 也,

This directive also affected the shorthand <?= before PHP 5.4.0, which is identical to <? echo 该指令还影响PHP 5.4.0之前的速记<?= ,与<? echo相同<? echo <? echo . <? echo Use of this shortcut required short_open_tag to be on. 要使用此快捷方式,必须启用short_open_tag。 Since PHP 5.4.0, <?= is always available. 从PHP 5.4.0开始, <?=始终可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM