for ($i=1; $i<=500; $i++)
{
echo "The number is " . $i . "<br />";
}
这是for循环,但我想要下一个输出:
This number is 1 is Red
This number is 2 is blue
This number is 3 is green
This number is 4 is orange
This number is 5 is Red
This number is 6 is blue
This number is 7 is green
This number is 8 is orange
This number is 9 is Red
This number is 10 is blue
This number is 11 is green
This number is 12 is orange
- 基本上红色,蓝色,绿色,橙色每四次重复一次 - 这是脑筋急转弯。
答案 0 :(得分:2)
这也是你的预告片
echo "<pre>";
$li = new LimitIterator(new InfiniteIterator(new ArrayIterator(array("red","blue","green","orange"))), 0, 500);
foreach ( $li as $key => $teaser ) {
printf("The number is %d %s %s", $key + 1, $teaser, PHP_EOL);
}
答案 1 :(得分:0)
使用模数函数根据计数器值决定要使用的颜色:
$colors=array ('red','blue','green','orange');
for ($i=1; $i<=500; $i++)
{
$color=$colors[$i%4];
echo "This number is ".$i." is ".$color."<br />\n";
}
答案 2 :(得分:0)
$colours = ["Red", "Blue", "Green", "Orange"];
for ($i=0; $i<500; $i++)
{
$colour = $i%4;
echo "The number is " . $i . " is ".$colours[$colour]. "<br />";
}