我想知道是否可以將for循環變量和函數組合在一起。所以它打印出在環路中的每個變量,我更喜歡簡單的解決方案,請Forloop變量和函數組合
$variable1 = 'hello';
$variable2 = 'goodnight';
for($x=0;$x<10;$x++){
echo $variable.$x;
}
我想知道是否可以將for循環變量和函數組合在一起。所以它打印出在環路中的每個變量,我更喜歡簡單的解決方案,請Forloop變量和函數組合
$variable1 = 'hello';
$variable2 = 'goodnight';
for($x=0;$x<10;$x++){
echo $variable.$x;
}
是的,它被稱爲foreach循環
$variables[0] = 'hello';
$variables[1] = 'goodnight';
foreach($variables as $variable){
echo $variable;
}
如果我理解正確的:
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>';
}
$variable1 = 'hello';
$variable2 = 'goodnight';
for($x=1;$x<=2;$x++){
$test="variable".$x;
echo $$test;
}
只需使用一個數組。應該是很簡單的
$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
謝謝你們,這是解決方案。
${"variable" . $x}
希望你們有一個美好的一天:)