2013-02-26 103 views
0

下面的代码是产生这样的结果:http://localhost/my_website/ contact-us结合变量和HTML PHP变量内

$base_url="http://localhost/my_website/"; 
echo $link= "$base_url contact-us "; 

但我想获得这样的结果:http://localhost/my_website/contact-us

我也曾尝试下面的代码

$base_url="http://localhost/my_website/"; 
echo $link= "$base_url.contact-us "; 

但结果是这样http://localhost/my_website/.contact-us

你能告诉我如何解决这个问题吗?

编辑

我很抱歉,我我以前不明确提到,我这里面临的具体问题。我认为上面的例子会帮助我的情况。其实我试图创建一个链接,我会在用户的电子邮件地址发送。

我的代码

$base_url="http://localhost/my_website/"; 
$random_hash="1"; 
echo $link=" 
<a href='$base_url account/confirm_registration/$random_hash' target='_blank'>$base_url account/confirm_registration/$random_hash</a>"; 

但它产生这样

http://localhost/my_website/ account/confirm_registration/1 
+1

你不知道如何使用连接字符'.':HTTP:// php.net/manual/en/language.operators.string.php – Stefan 2013-02-26 07:58:55

+0

非常感谢:)我访问了链接并找到了解决方案。我正在使用{}这个{$ base_url}帐户/ ...,并且它工作的很完美。谢谢:) – 2013-02-26 08:09:16

回答

0
echo $link = $base_url."contact-us"; 
+0

感谢您的回复,请检查我的问题的编辑部分 – 2013-02-26 07:53:56

+0

echo''.$base_url.''; //只需要用引号括起你的变量。 – Voitcus 2013-02-26 08:00:11

+0

非常感谢,您的代码有效,但我找到了更好的解决方案:)我正在使用'{}'这样的'{$ base_url}帐户/ ...' – 2013-02-26 08:07:11

0
$base_url="http://localhost/my_website/"; 
echo $link= $base_url."contact-us"; 
+0

请检查我的问题的编辑部分 – 2013-02-26 07:55:08

0

你只需把它分解

测试和工程:

$base_url="http://localhost/my_website/"; 

echo $link=$base_url."contact-us"; 
+0

您不需要'$ base_url'附近的引号。 – Stefan 2013-02-26 07:43:48

+0

你绝对是正确的先生! – Terning 2013-02-26 07:46:25

+1

然后你的答案变得与已经存在的答案一样... :) – Stefan 2013-02-26 07:47:06

0
$base_url="http://localhost/my_website/"; 
$link=$base_url."contact-us"; 
echo $link; 
+1

已经存在多个相似的答案。 – Stefan 2013-02-26 07:45:33

0

你需要了解基本的字符串连接:http://php.net/manual/en/language.operators.string.php

试试这个:

$base_url = "http://localhost/my_website/"; 
$random_hash = "1"; 
$url_page = "account/confirm_registration/$random_hash"; 
$url = $base_url . $url_page; 
$link = "<a href='$url'>$url</a>"; 

echo $link;