3

I get a variable from PHP code which is a webpage link, and want to use that in html tag. For example:

<?php 
  $link;

  ...
  $link = $client->api();
?>
 <a href= "<?php $link ?>" > LINK </a>

How can I get the link value in html tag? Thank you!

1
  • because you need to set $link value to href, you must use echo and not just $link. Commented Mar 15, 2013 at 8:46

6 Answers 6

3

Try this :

<a href= "<?php echo $link; ?>" > LINK </a>

echo it echo $link;

Alternatively, short hand would be:

<a href= "<?= $link; ?>" > LINK </a>
Sign up to request clarification or add additional context in comments.

Comments

3

You must echo the value ... also, don't forget to escape it:

<a href="<?php echo htmlspecialchars($link, ENT_QUOTES, 'UTF-8'); ?>">LINK</a>

Since PHP 5.4 it's also safe to use short open tags:

<a href="<?= htmlspecialchars($link, ENT_QUOTES, 'UTF-8'); ?>">LINK</a>

2 Comments

I would rather use htmlentities instead of htmlspecialchars
@Raubi No you would rather not use htmlentities() unless have a really good reason. For almost all situations htmlspecialchars() is sufficient and the correct way ™.
2

if you have html file then fist change the extention .php in place of .html

eg. you have test.html then change that to test.php

after that you use the same code as

<?php $link='http://stackoverflow.com' ?>

<a href= "<?php echo $link; ?>" > LINK </a>

Hope this will help you

Comments

0

Or in short way:

<a href="<?= $link; ?>">LINK</a>

Comments

0

this is work for me you have to call the function (echo) in HTML Tag ... that all , good luck.

enter <a href= "<?php echo $link; ?>" > LINK </a> here

Comments

0
<a href= "<?php echo $link; ?>" > LINK </a>

or

<?php

echo '<a href= "'.$link.'"> LINK </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.