如何将 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
How to add PHP variables to a href url
提问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.
你做错了几件事。
- In HTML you should quote attribute values such as href
- In PHP you're not concatenating anything, just echoing
- You forgot the link text
- For readability, you can (probably) use the
<?=shorthand instead of<?php echo <a /a>is broken tag syntax. Should be<a>...</a>
- 在 HTML 中,您应该引用属性值,例如 href
- 在 PHP 中你没有连接任何东西,只是回应
- 你忘记了链接文本
- 为了可读性,您可以(可能)使用
<?=简写代替<?php echo <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>

