PHP:传递php变量作为参数?
问题描述:
在我的php代码中,我需要调用一个脚本并传递一些参数。我如何传递php变量(作为参数)?PHP:传递php变量作为参数?
$string1="some value";
$string2="some other value";
header('Location: '...script.php?arg1=$string1&arg2=$string2');
感谢
答
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
答
像这样:
header("Location: http://www.example.com/script.php?arg1=$string1&arg2=$string2");
答
它应该工作,但换一个urlencode()来柜面有什么好笑打破网址:
header('Location: '...script.php?'.urlencode("arg1=".$string1."&arg2=".$string2).');
答
通过字符串连接:
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的元字符在他们
答
您可以使用功能
的urlencode()来部分是必需的(空格,&符号等)http_build_query
$query = http_build_query(array('arg1' => $string, 'arg2' => $string2));
header("Location: http://www.example.com/script.php?" . $query);
这应该像你显示的一样工作(只要它是一个有效的字符串,你需要删除第二个```) – 2011-02-16 14:18:14