2

I'm not sure if this is possible, but I have a counter variable which increases by one every time. Now I have a couple of variables above the counter which have a number after them e.g.

$var1=...
$var2=...
$var3=...

now inside the counter I would like the number after the variable to increase with the counter variable...

so like

for ( $counter = 1; $counter <= 3; $counter += 1) {
  $var=$var$counter;
}

What I don't want is a string, I just want a number after a variable to change everytime the counter variable goes up by one.

5 Answers 5

2

This is called a variable variable. Any time you think about using one, realise that you should use an array instead.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you but i can't use an array right now, if I changed some of the previous code which I got of a website, and I just want to test something out before I change it :)
@mario — it is also an answer since it links to the documentation which explains how to do what was asked.
1

Not sure what you mean by no string, but this should do it.

for ( $counter = 1; $counter <= 3; $counter += 1) {
   $varName = 'var' . $counter;
   $var=$$varName;
}

But I agree that a different method (array) should be used instead.

Comments

1

What are you trying to do with this?

I ask because it looks like you'd be better off using an array to achieve what you want. Then you could do something like:

for ( $counter = 1; $counter <= 3; $counter += 1) {
  $var[$counter] = $value;
}

Comments

0

You'll want to change that code to this:

for ( $counter = 1; $counter <= 3; $counter += 1) {
  ${"var".$counter} = $var;
}

Wrapping the string in braces allows you to use it to dynamically create a variable name, so we simply concatenate "var" with the counter value, and then use it as the new variable name.

However, note that doing this will create and initialize all of the variables along the way... So in this example, you'll end up with $var, $var1, and $var2 all equal to the same value. If this is not what you want, you will have to unset() all of the variables you don't want.

Also note, however, that this is not a "best practice" in most cases. May I ask why you need to do this? Perhaps an array would be more suitable..

Comments

0

You can do that, yet it's not recommended.

It looks like this:

for ( $counter = 1; $counter <= 3; $counter += 1) {
    $var = ${'var' . $counter};
}

2 Comments

This won't work. You're setting $var to the value of a variable that does not yet exist. Instead, you want to create a new variable with that name that is equal to $var. You need to flip them around the assignment operator, as I have done in my answer.
@BraedenP - You should be learning new things today. PHP is a loose typed language. Learn, young Skywalker, learn. This is your bread and butter!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.