I have a variable in a variable.

I have done this before but it is not working this time.

I have it in a for loop so my code is:

$abc1 = one;
 $abc2 = two;
 for($i = 0; $i < 2; $i++) {
  echo $$abc$i;  
 }

The out come should be "one two", correct?

The concept is correct? The code is correct? - to the best of my knowledge and references.

Any ideas? Thanks, Regards X.

Recommended Answers

All 8 Replies

The best way to do this would probably be an array:

$abc=array("value1","value2");

for($i = 0; $i < 2; $i++) {
echo $abc[$i];
}

Ya true xan but due to many reasons I cant (mysql - retrieving rows then calculations).

:(

Not sure exactly how variable variables work, but the PHP manual about them is here

So can anyone help me fix my code please?

I have a variable in a variable.

I have done this before but it is not working this time.

I have it in a for loop so my code is:

$abc1 = one;
 $abc2 = two;
 for($i = 0; $i < 2; $i++) {
  echo $$abc$i;  
 }

The out come should be "one two", correct?

The concept is correct? The code is correct? - to the best of my knowledge and references.

Any ideas? Thanks, Regards X.

The output will not be one two because $abc1 is one and not $abc0. Try this.

<?php 
$abc1 = "one";
 $abc2 = "two";
 for($i = 1; $i <= 2; $i++) {
  echo ${abc.$i}; //value of $i will be concatenated to string abc and then it will consider the whole as a variable  
 }
 ?>

Been really busy with this week with work, wont be able to test this until the weekend or next weekend. But if your 120% this will work ill mark this solved and give you credit and test it later.

Heh! I tested it before posting it here and I am 200% sure it will work..

commented: php guru once again :) +1

done work, it worked for me.

Now off to solve weirdo problems.

Might post them later.

Thanks nav once again :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.