在PHP中的PHP变量没有其他方式:<?php echo $ var; ?> [英] php variable in html no other way than: <?php echo $var; ?>
问题描述
我在混合的HTML和PHP中工作很多,大部分时间我只想要带有几个PHP变量的纯HTML,所以我的代码如下所示:
I work a lot in mixed HTML and PHP and most time I just want solid HTML with a few PHP variables in it so my code look like this:
<tr><td> <input type="hidden" name="type" value="<?php echo $var; ?>" ></td></tr>
这非常难看。
Which is quite ugly. Isn't there something shorter, more like the following?
<tr><td> <input type="hidden" name="type" value="$$var" ></td></tr>
这是可能的,但您会遇到(你必须全部用
'<<$ code>替换它们)并且布局不见了
This is possible to but you get stuck with the ""
(you have to replace them all with ''
) and the layout is gone
echo "<tr><td> <input type="hidden" name="type" value="$var" ></td></tr>"
有没有更好的办法?
Is there anything better?
推荐答案
<tr><td> <input type="hidden" name="type" value="<?= $var ?>" ></td></tr>
哪些(在PHP 5.4之前)需要在PHP配置中启用短标签。它的功能与您输入的代码完全相同;这些行在其内部实现方面的字面意思相同:
which (prior to PHP 5.4) requires short tags be enabled in your php configuration. It functions exactly as the code you typed; these lines are literally identical in their internal implementation:
<?= $var1, $var2 ?>
<?php echo $var1, $var2 ?>
这就是内置解决方案。有很多第三方模板库可以让您更容易地在输出中嵌入数据, smarty 是一个好地方开始。
That's about it for built-in solutions. There are plenty of 3rd party template libraries that make it easier to embed data in your output, smarty is a good place to start.
这篇关于在PHP中的PHP变量没有其他方式:<?php echo $ var; ?>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!