在 HTML 标签中使用 PHP 变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6130596/
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
Using PHP variables inside HTML tags?
提问by Jasper
I am pretty new to php but I'm stuck on this problem... Say i wait to put a link to another site with a given parameter, how do I do it correclty?
我对 php 很陌生,但我被困在这个问题上......假设我等待使用给定参数放置指向另一个站点的链接,我该怎么做才能正确?
This is what i have now:
这就是我现在所拥有的:
<html>
<body>
<?php
$param = "test";
echo "<a href="http://www.whatever.com/$param">Click Here</a>;
?>
</body>
</html>
回答by nicolaas
Well, for starters, you might not wanna overuse echo, because (as is the problem in your case) you can very easily make mistakes on quotation marks.
好吧,对于初学者来说,您可能不想过度使用 echo,因为(就像您的情况一样)您很容易在引号上出错。
This would fix your problem:
这将解决您的问题:
echo "<a href=\"http://www.whatever.com/$param\">Click Here</a>";
but you should really do this
但你真的应该这样做
<?php
$param = "test";
?>
<a href="http://www.whatever.com/<?php echo $param; ?>">Click Here</a>
回答by Nick
You can do it a number of ways, depending on the type of quotes you use:
您可以通过多种方式执行此操作,具体取决于您使用的引号类型:
echo "<a href='http://www.whatever.com/$param'>Click here</a>";
echo "<a href='http://www.whatever.com/{$param}'>Click here</a>";
echo '<a href="http://www.whatever.com/' . $param . '">Click here</a>';
echo "<a href=\"http://www.whatever.com/$param\">Click here</a>";
echo "<a href='http://www.whatever.com/$param'>Click here</a>";
echo "<a href='http://www.whatever.com/{$param}'>Click here</a>";
echo '<a href="http://www.whatever.com/' . $param . '">Click here</a>';
echo "<a href=\"http://www.whatever.com/$param\">Click here</a>";
Double quotes allow for variables in the middle of the string, where as single quotes are string literals and, as such, interpret everything as a string of characters -- nothing more -- not even \n
will be expanded to mean the new line character, it will just be the characters \
and n
in sequence.
双引号允许在字符串中间使用变量,因为单引号是字符串文字,因此,将所有内容解释为字符串——仅此而已——甚至\n
不会被扩展为表示换行符,它将只是字符\
和n
顺序。
You need to be careful about your use of whichever type of quoting you decide. You can't use double quotes inside a double quoted string (as in your example) as you'll be ending the string early, which isn't what you want. You can escape the inner double quotes, however, by adding a backslash.
您需要小心使用您决定的任何类型的引用。您不能在双引号字符串中使用双引号(如您的示例中所示),因为您将提前结束字符串,这不是您想要的。但是,您可以通过添加反斜杠来转义内部双引号。
On a separate note, you might need to be careful about XSSattacks when printing unsafe variables (populated by the user) out to the browser.
另外,在将不安全变量(由用户填充)打印到浏览器时,您可能需要小心XSS攻击。
回答by jhyry
There's a shorthand-type way to do this that I have been using recently. This might need to be configured, but it should work in most mainline PHP installations. If you're storing the link in a PHP variable, you can do it in the following manner based off the OP:
我最近一直在使用一种速记方式来做到这一点。这可能需要配置,但它应该适用于大多数主流 PHP 安装。如果您将链接存储在 PHP 变量中,您可以根据 OP 以下列方式进行操作:
<html>
<body>
<?php
$link = "http://www.google.com";
?>
<a href="<?= $link ?>">Click here to go to Google.</a>
</body>
</html>
This will evaluate the variable as a string, in essence shorthand for echo $link;
这将把变量作为一个字符串来求值,本质上是 echo $link 的简写;
回答by Christian Huber
I recommend using the short ' instead of ". If you do so, you wont longer have to escape the double quote (\").
我建议使用短的 ' 而不是 "。如果你这样做,你将不再需要转义双引号 (\")。
In that case you would write
在那种情况下,你会写
echo '<a href="http://www.whatever.com/'. $param .'">Click Here</a>';
But look onto nicolaas' answer "what you really should do" to learn how to produce cleaner code.
但是请查看 nicolaas 的回答“您真正应该做什么”以了解如何生成更清晰的代码。
回答by TerenceHymanson
HI Jasper,
嗨贾斯珀,
you can do this:
你可以这样做:
<?
sprintf("<a href=\"http://www.whatever.com/%s\">Click Here</a>", $param);
?>
回答by Sean Walsh
You can embed a variable into a double quoted string like my first example, or you can use concantenation(the period) like in my second example:
您可以像我的第一个示例一样将变量嵌入到双引号字符串中,或者您可以像我的第二个示例一样使用 concantenation(句点):
echo "<a href=\"http://www.whatever.com/$param\">Click Here</a>";
echo "<a href=\"http://www.whatever.com/$param\">Click Here</a>";
echo '<a href="http://www.whatever.com/' . $param . '">Click Here</a>';
echo '<a href="http://www.whatever.com/' . $param . '">Click Here</a>';
Notice that I escaped the double quotes inside my first example using a backslash.
请注意,我使用反斜杠对第一个示例中的双引号进行了转义。
回答by Emyr
Heredoc may be an option, see example 2 here: http://php.net/manual/en/language.types.string.php
Heredoc 可能是一个选项,请参见此处的示例 2:http: //php.net/manual/en/language.types.string.php