2

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

5
  • 1
    Make 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> Commented Jan 31, 2014 at 20:23
  • 1
    Your syntax is broken. You want to get rid of the quotes inside the PHP part (but wrap the entire href attribute in them) Commented Jan 31, 2014 at 20:24
  • <a href="example.com/uid= <?php echo $uid?"></a> Commented Jan 31, 2014 at 20:25
  • This 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. Commented Jan 31, 2014 at 20:26
  • @user3245415 you're supposed to pick the answer that helps you most at some point. Are you still stuck on this? Commented Feb 5, 2014 at 0:15

8 Answers 8

8

Try this,

<?php
@session_start();
$uid= $_SESSION['userid'];
?>
<a href="http://example.com/?uid=<?php echo $uid; ?>" >Your link text</a> 
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1

Perhaps something like this:

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

1 Comment

Syntax is wrong - missing closing bracket for a tag and the double-quote isn't needed.
1

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

Comments

1

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!

Comments

0

This will work

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

Comments

0

Try this:

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

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.