简体   繁体   English

在html标签中使用PHP变量

[英]using PHP variables in html tag

I get a variable from PHP code which is a webpage link, and want to use that in html tag. 我从PHP代码获取一个变量,这是一个网页链接,并希望在html标记中使用它。 For example: 例如:

<?php 
  $link;

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

How can I get the link value in html tag? 如何获取html标记中的链接值? Thank you! 谢谢!

Try this : 尝试这个 :

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

echo it echo $link; echo echo $link;

Alternatively, short hand would be: 或者,简写为:

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

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 : 从PHP 5.4开始,使用短开标签也是安全的:

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

if you have html file then fist change the extention .php in place of .html 如果你有html文件然后拳头改为扩展.php代替.html

eg. 例如。 you have test.html then change that to test.php 你有test.html然后将其更改为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 希望这个能对您有所帮助

或者简短地说:

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

这对我有用,你必须在HTML标签中调用函数(echo)...祝你好运。

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

or 要么

<?php

echo '<a href= "'.$link.'"> LINK </a>';

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM