John Smith
John Smith

Reputation: 69

PHP dynamically reference variable in string literal

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}";
}

Upvotes: 2

Views: 3068

Answers (1)

nice ass
nice ass

Reputation: 16719

This should do it:

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

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

Upvotes: 6

Related Questions