How do I combine variables/text?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Swede78
Forum Contributor
Posts: 198
Joined: Wed Mar 12, 2003 12:52 pm
Location: IL

How do I combine variables/text?

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

the dot operator

Post 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
Swede78
Forum Contributor
Posts: 198
Joined: Wed Mar 12, 2003 12:52 pm
Location: IL

Post by Swede78 »

Thanks! Worked just as I wanted.
Post Reply