2011-02-16 167 views
0

在我的php代碼中,我需要調用一個腳本並傳遞一些參數。我如何傳遞php變量(作爲參數)?PHP:傳遞php變量作爲參數?

$string1="some value"; 
$string2="some other value"; 
header('Location: '...script.php?arg1=$string1&arg2=$string2'); 

感謝

+1

這應該像你顯示的一樣工作(只要它是一個有效的字符串,你需要刪除第二個```) – 2011-02-16 14:18:14

回答

4
header('Location: ...script.php?arg1=' . $string1 . '&arg2=' . $string2); 
+0

不值得一個新的答案,但雙引號也是可能的:`header(「Location:http: //example.com/script.php?arg1=$string1&arg2=$string2「);` – KingCrunch 2011-02-16 14:19:45

0

像這樣:

header("Location: http://www.example.com/script.php?arg1=$string1&arg2=$string2"); 
0

它應該工作,但換一個urlencode()來櫃面有什麼好笑打破網址:

header('Location: '...script.php?'.urlencode("arg1=".$string1."&arg2=".$string2).'); 
4

通過字符串連接:

header('Location: script.php?arg1=' . urlencode($string1) . '&arg2=' . urlencode($string2)); 

或字符串插值

$string1=urlencode("some value"); 
$string2=urlencode("some other value"); 
header("Location: script.php?arg1={$string1}&arg2={$string2}"); 

就個人而言,我更喜歡第二種風格。它的眼睛容易得多,錯誤的報價和/或.的機會更少,而且使用任何體面的語法高亮編輯器,變量的顏色將與字符串的其餘部分不同。如果你的價值觀有什麼樣的URL的元字符在他們

3

您可以使用功能

的urlencode()來部分是必需的(空格,&符號等)http_build_query

$query = http_build_query(array('arg1' => $string, 'arg2' => $string2)); 

header("Location: http://www.example.com/script.php?" . $query);