html 中的 php 变量别无他法: <?php echo $var; ?>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2150238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php variable in html no other way than: <?php echo $var; ?>
提问by matthy
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:
我在混合 HTML 和 PHP 中做了很多工作,大多数时候我只想要带有一些 PHP 变量的纯 HTML,所以我的代码如下所示:
<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>
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?
有更好的吗?
回答by meagar
There's the short tag version of your code, which is now completely acceptable to usedespite antiquated recommendations otherwise:
您的代码有短标签版本,尽管有过时的建议,但现在完全可以使用它:
<input type="hidden" name="type" value="<?= $var ?>" >
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:
其中(PHP 5.4 之前)需要在您的 php 配置中启用短标签。它的功能与您键入的代码完全相同;这些行在其内部实现中实际上是相同的:
<?= $var1, $var2 ?>
<?php echo $var1, $var2 ?>
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, smartyis a good place to start.
这就是内置解决方案的内容。有很多 3rd 方模板库可以更轻松地将数据嵌入到您的输出中,smarty是一个不错的起点。
回答by code_burgar
Use the HEREDOCsyntax. You can mix single and double quotes, variables and even function calls with unaltered / unescaped html markup.
使用HEREDOC语法。您可以将单引号和双引号、变量甚至函数调用与未更改/未转义的 html 标记混合使用。
echo <<<MYTAG
<tr><td> <input type="hidden" name="type" value="$var1" ></td></tr>
<tr><td> <input type="hidden" name="type" value="$var2" ></td></tr>
<tr><td> <input type="hidden" name="type" value="$var3" ></td></tr>
<tr><td> <input type="hidden" name="type" value="$var4" ></td></tr>
MYTAG;
回答by gcb
I really think you should adopt Smarty template engine as a standard php lib for your projects.
我真的认为您应该采用 Smarty 模板引擎作为您项目的标准 php 库。
Name: {$name|capitalize}<br>
回答by Coly Moore
In a php section before the HTML section, use sprinf() to create a constant string from the variables:
在 HTML 部分之前的 php 部分中,使用 sprinf() 从变量创建一个常量字符串:
$mystuff = sprinf("My name is %s and my mother's name is %s","Suzy","Caroline");
Then in the HTML section you can do whatever you like, such as:
然后在 HTML 部分,您可以做任何您喜欢的事情,例如:
<p>$mystuff</p>
回答by Jordan Running
There are plenty of templating systems that offer more compact syntax for your views. Smartyis venerable and popular. This articlelists 10 others.
回答by Niels Bom
I'd advise against using shorttags, see Are PHP short tags acceptable to use?for more information on why.
我建议不要使用短标签,请参阅是否可以使用 PHP 短标签?有关原因的更多信息。
Personally I don't mind mixing HTML and PHP like so
我个人不介意像这样混合 HTML 和 PHP
<a href="<?php echo $link;?>">link description</a>
As long as I have a code-editor with good syntax highlighting, I think this is pretty readable. If you start echoing HTML with PHP then you lose all the advantages of syntax highlighting your HTML. Another disadvantage of echoing HTML is the stuff with the quotes, the following is a lot less readable IMHO.
只要我有一个具有良好语法突出显示的代码编辑器,我认为这很容易阅读。如果您开始使用 PHP 回显 HTML,那么您将失去语法突出显示 HTML 的所有优势。回显 HTML 的另一个缺点是带有引号的内容,恕我直言,以下内容的可读性要差得多。
echo '<a href="'.$link.'">link description</a>';
The biggest advantage for me with simple echoing and simple looping in PHP and doing the rest in HTML is that indentation is consistent, which in the end improves readability/scannability.
对我来说,在 PHP 中使用简单的回显和简单循环并在 HTML 中完成其余部分的最大优势是缩进是一致的,这最终提高了可读性/可扫描性。

