PHP dynamically reference variable in string literal

我的梦境 提交于 2019-12-02 04:52:54

问题


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:


This should do it:

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

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



来源:https://stackoverflow.com/questions/17203722/php-dynamically-reference-variable-in-string-literal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!