Web Hosting Talk







View Full Version : combining two variables, php


jasong
06-22-2004, 02:38 PM
Hey,

I'm trying to combine two variables together to get what it is (ok that made no sense)...

[PHP}
$something_1 = pencil;
$something_2 = book

$X = "1";
while (X <= 2) {
print "$something_.$X" <BR>;
$X++;
}
[PHP]

(if there is something wrong with the loop ignore it, i just put it there to show what i'm doing)
Understand? I want it to call $something_1, and then $something_2.
I've tried a bunch of different combinations of ()'s and "." 's but no such luck.
how do i string the two variables together?

thanks,
Jason

SeņorAmor
06-22-2004, 02:44 PM
Use curly braces { }


$x = 1;
while ($x <= 2) {
print ${"something_${x}"} . "<BR>";
$x++;
}

I'm pretty sure that'd work.

t c
06-22-2004, 06:34 PM
$something_1 = pencil;
$something_2 = book

$X = "1";
while (X <= 2) {
print $something_ . $X . " <BR />";
$X++;
}

I just glanced at your code and thought about trying it this way.

Burhan
06-23-2004, 01:34 AM
You need variable variables (http://www.php.net/manual/en/language.variables.variable.php).

t c
06-23-2004, 05:12 AM
fyrestrtr, that's very nice! Something for me to look into and play around with. :)

dnn
06-23-2004, 08:34 AM
Array , http://us3.php.net/types.array , what do you guys think ?