[英]Using PHP variables inside HTML tags?
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>
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.好吧,对于初学者来说,您可能不想过度使用回声,因为(就像您的问题一样)您很容易在引号上出错。
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>
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>";
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 XSS attacks when printing unsafe variables (populated by the user) out to the browser.另外,在将不安全变量(由用户填充)打印到浏览器时,您可能需要小心XSS攻击。
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.
这可能需要配置,但它应该适用于大多数主线 PHP 安装。 If you're storing the link in a PHP variable, you can do it in the following manner based off the OP:
如果您将链接存储在 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; 的简写;
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 的回答“你真正应该做什么”来学习如何生成更清晰的代码。
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:您可以像我的第一个示例一样将变量嵌入到双引号字符串中,或者您可以像我的第二个示例中那样使用串联(句点):
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.请注意,我在第一个示例中使用反斜杠转义了双引号。
HI Jasper,嗨贾斯珀,
you can do this:你可以这样做:
<?
sprintf("<a href=\"http://www.whatever.com/%s\">Click Here</a>", $param);
?>
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.