How to add PHP variables to a href url
30,071
Solution 1
Try this,
<?php
@session_start();
$uid= $_SESSION['userid'];
?>
<a href="http://example.com/?uid=<?php echo $uid; ?>" >Your link text</a>
Solution 2
echo "<a href=\"http://example.com/?uid=$uid\">link description</a>";
Solution 3
Perhaps something like this:
print '<a href=http://example.com/?uid=' . $uid . '>Link</a>';
Solution 4
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!
Solution 5
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>';
Author by
user3245415
Updated on February 05, 2020Comments
-
user3245415 over 4 years
Im trying to add a PHP variable to a href url
This is the code:
PHP
$uid= $_SESSION['userid'];
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=.
-
Admin over 10 yearsMake sure you have
session_start()
at the top of your script, and the syntax should look like<a href="http://example.com/uid=<?php echo $uid; ?>">...</a>
-
Pekka over 10 yearsYour syntax is broken. You want to get rid of the quotes inside the PHP part (but wrap the entire
href
attribute in them) -
Jack over 10 years<a href="example.com/uid= <?php echo $uid?"></a>
-
Marc B over 10 yearsThis depends ENTIRELY on how you're using that html. Is it being assigned to a php string? Then echo is the totally wrong tool. Is it direct output "out" of php mode? Then it'd work, if you fixed your html syntax.
-
Paul Gregory over 10 years@user3245415 you're supposed to pick the answer that helps you most at some point. Are you still stuck on this?
-
-
Jack over 10 yearsSyntax is wrong - missing closing bracket for a tag and the double-quote isn't needed.