Domanda

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}";
}
È stato utile?

Soluzione

This should do it:

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

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top