2

I have multiple PHP variables in the form

$number1, $number2, $number3 and so on...

I would like to dynamically reference these inside of a loop to retrieve information from them, but am not sure how to reference the static variable dynamically. Ex:

for($i = 1; $i <= 10; $i++) {
    //The first number to be printed should be the value from
    //$number1 not $number concatenated to $i

    //here are some of the strings I tried:
    echo "$number$i";
    echo "{$number}$i";
    echo "{$number}{$i}";
}

1 Answer 1

6

This should do it:

echo ${"number{$i}"};

But why not use arrays instead? Having $number[$i] is much more readable...

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

1 Comment

Thanks, thats worked! And I would use arrays, but I am only supposed to change a function that manipulates variables already there.

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.