2015-08-28 54 views
0

我想知道是否可以将for循环变量和函数组合在一起。所以它打印出在环路中的每个变量,我更喜欢简单的解决方案,请Forloop变量和函数组合

$variable1 = 'hello'; 
$variable2 = 'goodnight'; 

for($x=0;$x<10;$x++){ 
    echo $variable.$x; 
} 

回答

2

是的,它被称为foreach循环

$variables[0] = 'hello'; 
$variables[1] = 'goodnight'; 

foreach($variables as $variable){ 
    echo $variable; 
} 
0

如果我理解正确的:

function variables($x) 
{ 
    switch($x) 
    { 
    case 1: return 'hello'; break; 
    case 2: return 'good night'; break; 
    } 
} 

for($x=0;$x<10;$x++){ 
    echo variables($x).'<br>'; 
} 
0
$variable1 = 'hello'; 
$variable2 = 'goodnight'; 

for($x=1;$x<=2;$x++){ 
    $test="variable".$x; 
    echo $$test; 
} 
0

只需使用一个数组。应该是很简单的

$var = array('mon', 'tue', 'wed', 'thu', 'fri'); 

for($x=0; $x<count($var); $x++) 
{ 
    echo $var[$x]; 
    echo "<br>"; 
} 

OUTPUT:

mon 
tue 
wed 
thu 
fri 
2

谢谢你们,这是解决方案。

${"variable" . $x} 

希望你们有一个美好的一天:)