0

In my php code I need to invoke a script and passing it some arguments. How can I pass php variables (as arguments) ?

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

thanks

1
  • 1
    It should work exactly as you show (as long as it's a valid string, for which you would need to remove the second ') Commented Feb 16, 2011 at 14:18

5 Answers 5

4
header('Location: ...script.php?arg1=' . $string1 . '&arg2=' . $string2);
Sign up to request clarification or add additional context in comments.

1 Comment

Not worth a new answer, but double quotes are also possible: header("Location: http://example.com/script.php?arg1=$string1&arg2=$string2");
4

Either via string concatenation:

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

Or string interpolation

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

Personally, I prefer the second style. It's far easier on the eyes and less chance of a misplaced quote and/or ., and with any decent syntax highlighting editor, the variables will be colored differently than the rest of the string.

The urlencode() portion is required if your values have any kind of url-metacharacters in them (spaces, ampersands, etc...)

Comments

3

You could use the function http_build_query

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

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

Comments

0

Like this:

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

Comments

0

It should work, but wrap a urlencode() incase there is anything funny breaking the url:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.