0

I am trying to get a variable to increment by adding a counter to the end of it. Here is the code.

$counter = 0;
while($results = $query->fetch_assoc()){
        $var . ++$counter = $results['row'];
    } // while loop

My target outcome...

$var1 = result 1
$var2 = result 2
$var3 = result 3

It is trying to set $counter = result.

2
  • Your code is invalid, you cannot assign to an expression like $var . ++$counter. Commented May 28, 2015 at 10:40
  • I know. That is why I'm asking in here. It obviously didnt work. If I knew how to do it, it would be pointless to ask the question. Commented May 28, 2015 at 10:44

1 Answer 1

2

To make a variable variable you can use {}:

$counter = 0;
while($results = $query->fetch_assoc()){
   ${"var".++$counter} = $results['row'];
} // while loop
Sign up to request clarification or add additional context in comments.

3 Comments

wow. I should have known that. I just did something similar the other day. Thanks!
Just a side note (For your information only, @KDJ), this works as well with strings: "{$result['row']} is the result of {$var} at counter {++$counter}";
that seemed to make $var an array. Why is this?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.