创建一个for循环,以1为增量回显一系列数字

时间:2013-10-23 10:37:04

标签: php for-loop

根据以下内容的输出,我想知道是否有人可以帮助我:

  $monthsOfTheYear = range(1, 12);
  print_r ($monthsOfTheYear);

我理解上面的过程,但是用for循环重建这个原理的最有效方法是什么?到目前为止,我已经提出了以下内容:

  $months = ("January", "February", "March", "April", "May", "June", "July", "August", September", "October", "November", "December") {

  for($months{0} = 1, $months{11} = 12, /*????*/) {
      echo "/*????*/";
  } }

如果可能的话,我想知道如何在循环代码中实现这一点,但我怀疑它可能需要更复杂的解决方案。

4 个答案:

答案 0 :(得分:1)

你是不是想这样做。可能但是我不确定你是否想要这个。请让你的问题更清楚一点。试着添加样本输出。

$months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
for($i = 0;$i < 12; $i++){
 echo $months[$i].'<br>';
}

答案 1 :(得分:0)

for ($i = 0; $i < 12; $i++) {
    echo $monthsOfTheYear[$i];
}

答案 2 :(得分:0)

许多可能的解决方案之一是

foreach(array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December") as $month) {
    echo $month . "<br>";
}

答案 3 :(得分:0)

你的意思是这样吗?

$months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

for ($i=0; $i<count($months); $i++) {
  echo "$months[$i]\n";
}

请参阅http://3v4l.org/T740C

的输出