如何在 PHP 中添加 href 链接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15455593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:16:22  来源:igfitidea点击:

How to add a href link in PHP?

phphtmlhref

提问by Doug Fir

I'm very new to PHP and trying to make a site update on the fly.

我对 PHP 非常陌生,并试图即时更新站点。

I need to update the code so that the link will direct to www.otherwebsite.com. Currently the link directs to the current websites RSS feed.

我需要更新代码,以便链接指向 www.otherwebsite.com。目前,该链接指向当前网站的 RSS 提要。

Here is the original div:

这是原始的div:

<div style="float:right;">
<a href="<?php echo url::site(); ?>feed/"><img src="<?php echo url::file_loc('img'); ?>media/img/icon-feed.png" style="vertical-align: middle;" border="0"></a>

I need to alter this to point to www.someotherwebsite.com. It looked simple enough. Here is what I tried:

我需要将其更改为指向 www.someotherwebsite.com。它看起来很简单。这是我尝试过的:

<div style="float:right;">
    <a href="<?php echo www.someotherwebsite.com"><img src="<?php echo url::file_loc('img'); ?>media/img/twitter.png" style="vertical-align: middle;" border="0"></a>

That never worked, instead I received an error message concerning the line where I had made this edit.

这从未奏效,相反,我收到了一条关于我进行此编辑的行的错误消息。

How would I update this?

我将如何更新这个?

回答by DACrosby

Looks like you missed a few closing tags and you nshould have "http://" on the front of an external URL. Also, you should move your styles to external style sheets instead of using inline styles.

看起来您错过了一些结束标记,并且您应该在外部 URL 的前面加上“http://”。此外,您应该将样式移动到外部样式表,而不是使用内联样式。

.box{
  float:right;
}
.box a img{
  vertical-align: middle;
  border: 0px;
}

<div class="box">
    <a href="<?php echo "http://www.someotherwebsite.com"; ?>">
        <img src="<?php echo url::file_loc('img'); ?>media/img/twitter.png" alt="Image Decription">
    </a>
</div>

As noted in other comments, it may be easier to use straight HTML, depending on your exact setup.

正如其他评论中所述,根据您的具体设置,使用纯 HTML 可能更容易。

<div class="box">
    <a href="http://www.someotherwebsite.com">
        <img src="file_location/media/img/twitter.png" alt="Image Decription">
    </a>
</div>

回答by icktoofay

There is no need to invoke PHP for this. Just put it directly into the HTML:

无需为此调用 PHP。直接把它放到 HTML 中:

<a href="http://www.example.com/">...

回答by Aliihsan Erdem

you have problems with " :

你有“问题”:

 <a href=<?php echo "'www.someotherwebsite.com'><img src='". url::file_loc('img'). "media/img/twitter.png' style='vertical-align: middle' border='0'></a>"; ?>

回答by Max

Just do it in HTML

只需在 HTML 中进行

<a href="https://www.google.com">Google</a>