Jump to content


Photo

2 variables stuck together


  • Please log in to reply
4 replies to this topic

#1 briant

briant
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts

Posted 25 September 2006 - 05:38 PM

$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.

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 25 September 2006 - 05:45 PM

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:
${'myvar' . $num} = 'blah blah';

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


#3 briant

briant
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts

Posted 25 September 2006 - 05:48 PM

I LOVE YOU FOREVER! Thanks so much, exactly what I was looking for. Wildteen88, you are da bomb, always helping everyone out. Thanks again

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 25 September 2006 - 05:54 PM

I forgot the test var. This is what my example should be:
$test = 'myvar';
$num = 1;

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

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

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

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 25 September 2006 - 06:13 PM

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

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users