Jump to content

Archived

This topic is now archived and is closed to further replies.

briant

2 variables stuck together

Recommended Posts

$num = 1;
$test$num = "blah blah";
print $test1;

$test$num, how can i have 2 variables stuck together?

I tried adding a period $test.$num but that didn't work.

Share this post


Link to post
Share on other sites
Do you want to create a variable which is called what $test holds and add $num to the name. For example:

$test - holds myvar
$num - holds the number 1

You can to create a variabled valled myvar1

You'll do this:
[code=php:0]${'myvar' . $num} = 'blah blah';

// echo the variable that was created above.
echo $myvar1;[/code]

Share this post


Link to post
Share on other sites
I LOVE YOU FOREVER! Thanks so much, exactly what I was looking for. Wildteen88, you are da bomb, always helping everyone out. Thanks again

Share this post


Link to post
Share on other sites
I forgot the test var. This is what my example should be:
[code=php:0]$test = 'myvar';
$num = 1;

${$test. $num} = 'blah blah';

// echo the variable that was created above.
echo $myvar1;[/code]

However its stull the same. Just that I had myvar 'hardcoded' in.

Share this post


Link to post
Share on other sites
However, instead of constructing varables with names like "$myvar1", "$myvar2" and having
[code]
$myvar1 = 'blah';
$myvar2 = 'blah blah';
[/code]
it's a lot easier and more convenient to store in an array
[code]
$myvar = array();
$myvar[1] = 'blah';
$myvar[2] = 'blah blah';
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.