如何将 PHP 变量添加到 href url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21489343/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:11:20  来源:igfitidea点击:

How to add PHP variables to a href url

phphtmlhref

提问by user3245415

Im trying to add a PHP variable to a href url

我正在尝试将 PHP 变量添加到 href url

This is the code:

这是代码:

PHP

PHP

$uid= $_SESSION['userid'];

HTML

HTML

<a href=http://example.com/uid= <?php echo ".$uid."?> /a>

How do you add, when I do it, this is what it redirect too: http://example.com/uid=.

你如何添加,当我这样做时,这也是它重定向的内容:http: //example.com/uid=

回答by Krish R

Try this,

尝试这个,

<?php
@session_start();
$uid= $_SESSION['userid'];
?>
<a href="http://example.com/?uid=<?php echo $uid; ?>" >Your link text</a> 

回答by verbumSapienti

echo "<a href=\"http://example.com/?uid=$uid\">link description</a>";

回答by Sachin Sridhar

This is one of the most helpful

这是最有帮助的之一

echo "<td>".($tripId != null ? "<a target=\"_blank\"href=\"http://www.rooms.com/r/trip/".$tripId."\">".$tripId."</a>" : "-")."</td>";

It will work!

它会起作用!

回答by urvashi joshi

try this

尝试这个

<?php 
$url = 'https://www.w3schools.com/php/';
?>

<a href="<?php echo $url;?>">PHP 5 Tutorial</a>

Or for PHP 5.4+ (<?= is the PHP short echo tag):
<a href="<?= $url ?>">PHP 5 Tutorial</a>

or 
echo '<a href="' . $url . '">PHP 5 Tutorial</a>';

回答by Stephen R

You're doing a couple things wrong.

你做错了几件事。

  1. In HTML you should quote attribute values such as href
  2. In PHP you're not concatenating anything, just echoing
  3. You forgot the link text
  4. For readability, you can (probably) use the <?=shorthand instead of <?php echo
  5. <a /a>is broken tag syntax. Should be <a>...</a>
  1. 在 HTML 中,您应该引用属性值,例如 href
  2. 在 PHP 中你没有连接任何东西,只是回应
  3. 你忘记了链接文本
  4. 为了可读性,您可以(可能)使用<?=简写代替<?php echo
  5. <a /a>是损坏的标签语法。应该<a>...</a>

End result is this:

最终结果是这样的:

<a href="http://example.com/uid=<?=$uid?>">Link Text</a>

回答by Stephen R

Perhaps something like this:

也许是这样的:

print '<a href=http://example.com/?uid=' . $uid . '>Link</a>';

回答by Sean Keane

This will work

这将工作

 <a href=http://example.com/?uid= <?php echo $uid ?> Myurl</a>

回答by dhavald

Try this:

尝试这个:

<a href=http://example.com/uid= <?=$uid?> /a>