在PHP中从字符串调用变量


Calling a variable from string in PHP

我需要以最短的方式调用类似的变量。

$test = new stdClass();    
$test->var0=0;    
$test->var1=1;    
$test->var2=2; 

现在我需要回显循环中的所有3个变量,而不是像这样:

echo $test->var0;   
echo $test->var1;
echo $test->var2;

试试这个-

for($i = 0; $i < 3; $i++) {
    echo $test->{'var' . $i};
}