[英]Add PHP variable inside echo statement as href link address?
I'm trying to use a PHP variable to add a href value for a link in an echo statement.我正在尝试使用 PHP 变量为 echo 语句中的链接添加 href 值。
Here's a simplified version of the code I want to use.这是我要使用的代码的简化版本。 I know that I can't just add the variable into the echo statement, but I can't seem to find an example anywhere that works.我知道我不能只是将变量添加到 echo 语句中,但我似乎无法在任何地方找到一个有效的例子。
$link_address = '#';
echo '<a href="$link_address">Link</a>';
Try like试试像
HTML in PHP : PHP 中的 HTML :
echo "<a href='".$link_address."'>Link</a>";
Or even you can try like或者甚至你可以尝试像
echo "<a href='$link_address'>Link</a>";
Or you can use PHP in HTML like或者您可以在 HTML 中使用 PHP,例如
PHP in HTML : HTML 中的 PHP :
<a href="<?php echo $link_address;?>"> Link </a>
you can either use你可以使用
echo '<a href="'.$link_address.'">Link</a>';
or要么
echo "<a href=\"$link_address\">Link</a>';
if you use double quotes you can insert the variable into the string and it will be parsed.如果您使用双引号,您可以将变量插入字符串中,它将被解析。
Basically like this,基本上是这样的
<?php
$link = ""; // Link goes here!
print "<a href="'.$link.'">Link</a>";
?>
就这么简单: echo '<a href="'.$link_address.'">Link</a>';
You can use one and more echo statement inside href您可以在 href 中使用一个或多个 echo 语句
<a href="profile.php?usr=<?php echo $_SESSION['firstname']."&email=". $_SESSION['email']; ?> ">Link</a>
link : "/profile.php?usr=firstname&email=email"链接:“/profile.php?usr=firstname&email=email”
This worked much better in my case.这在我的情况下效果更好。
HTML in PHP: <a href=".$link_address.">Link</a>
PHP 中的 HTML: <a href=".$link_address.">Link</a>
The safest way to generate links in PHP is to use the built-in function http_build_query()
.在 PHP 中生成链接最安全的方法是使用内置函数http_build_query()
。 This function is very easy to use and takes an array as an argument.此函数非常易于使用,并以数组作为参数。
To create a dynamic link simply echo out the result of http_build_query()
like so:要创建动态链接,只需像这样回显http_build_query()
的结果:
$data = [
'id' => $id,
'name' => $name
];
echo '<a href="index.php?'.http_build_query($data).'">Link</a>';
如果要以表格形式打印,则可以使用:
echo "<tr> <td><h3> ".$cat['id']."</h3></td><td><h3> ".$cat['title']."<h3></</td><td> <h3>".$cat['desc']."</h3></td><td><h3> ".$cat['process']."%"."<a href='taskUpdate.php' >Update</a>"."</h3></td></tr>" ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.