2016-01-13 54 views
1

我需要以最短的方式調用類似的變量。在PHP中調用字符串中的變量

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

,現在我需要回聲在週期中的所有3個變量,而不是像這樣:

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

也許使用數組? – jmattheis

回答

3

試試這個 -

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

非常感謝,拯救了我的一天:) – eddieh

+0

@eddieh接受答案,這將有助於其他同樣面臨同樣問題的人點擊左邊的勾號 – devpro