PHP:将php变量作为参数传递?

时间:2011-02-16 14:16:54

标签: php

在我的PHP代码中,我需要调用一个脚本并传递一些参数。我怎样才能传递php变量(作为参数)?

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

感谢

5 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分: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-metacharacters(空格,符号等),则需要urlencode()部分

答案 2 :(得分:3)

您可以使用http_build_query

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

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

答案 3 :(得分:0)

像这样:

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

答案 4 :(得分:0)

它应该可以工作,但是包装一个urlencode(),这有什么好玩的打破网址:

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