user3245415
user3245415

Reputation: 65

How to add PHP variables to a href url

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=.

Upvotes: 2

Views: 35233

Answers (8)

Stephen R
Stephen R

Reputation: 3897

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>

End result is this:

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

Upvotes: 0

Sachin Sridhar
Sachin Sridhar

Reputation: 445

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!

Upvotes: 1

urvashi joshi
urvashi joshi

Reputation: 21

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>';

Upvotes: 1

pulsar
pulsar

Reputation: 987

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

Upvotes: 2

user3111737
user3111737

Reputation:

Perhaps something like this:

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

Upvotes: 1

dhavald
dhavald

Reputation: 554

Try this:

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

Upvotes: 0

Sean Keane
Sean Keane

Reputation: 411

This will work

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

Upvotes: 0

Krish R
Krish R

Reputation: 22711

Try this,

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

Upvotes: 8

Related Questions