2013-02-26 97 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;