du997562 2012-08-02 01:55
浏览 25
已采纳

PHP动态命名和访问变量

$var1 = 'First Variable';
$var2 = 'Second Variable';
$var3 = 'Third Variable';

$number = 2;

$variable_to_get = 'var'.$number;

Lets say you want to echo out a variable depending on what $number is; e.g. in this case $number is 2 so we want to echo out $var2. How would you go about doing this without using switch statements / ifs; just directly naming a variable and showing the result?

  • 写回答

5条回答 默认 最新

  • dtotuki47568 2012-08-02 02:00
    关注

    variable variable

    echo ${'var'.$number};
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?