Page 1 of 1

How do I combine variables/text?

Posted: Fri Apr 25, 2003 2:39 pm
by Swede78
I've been looking and looking and cannot seem to find how to do this at php.net. I just want to create a variable that combines (not mathematically) several variables and text together.

For example:
$variableA = "sample1"
$variableB = "sample2"

$combo = "VariableA=" & $variableA & "&VariableB=" & $variableB;

I want the $combo variable to produce this: "VariableA=sample1&VariableB=sample2"

Using the "&" character to combine them works in ASP, but I can't find how to do this in PHP. Please help.

Posted: Fri Apr 25, 2003 2:55 pm
by volka

the dot operator

Posted: Fri Apr 25, 2003 3:00 pm
by phpScott
I saw in your message that you actually used the correct operator 5 times you just didn't know it.

In php we use the dot operator for piecing lines togther.
Like so:

Code: Select all

$combo = "VariableA= " . $variableA . "&VariableB= " . $variableB . "<br />;
print $combo;
Should result in VariableA=sample1&VariableB=sample2

Also php is some what forgiving so you could also get away with

Code: Select all

$combo = "VariableA= $variableA &VariableB=  $variableB <br />;
And you would end up with the same result.

phpScott

Posted: Fri Apr 25, 2003 5:06 pm
by Swede78
Thanks! Worked just as I wanted.