如何计算For循环的结果

时间:2017-06-29 04:54:45

标签: php loops

我们如何计算For Loop的结果

$starting_year=2001;
$ending_year=2015;

for ($x = $starting_year; $x <= $ending_year;$x++) {
    echo "$x <br />";
} 

我该如何计算这一代?例如,上面应该是5

3 个答案:

答案 0 :(得分:1)

尝试使用 获取代数 循环,但我也有一个问题,例如为什么你需要 for 循环这里只是减去去年到第一年的价值来生成

$starting_year = 2001;
$ending_year = 2015;
$generation = 0;
for ($x = $starting_year; $x <= $ending_year; $x++) {
   echo "$x <br />";
   $generation++;
} 
echo "No of generation $generation";

答案 1 :(得分:1)

希望它会对你有帮助,

<?php
  $starting_year = 2001;
  $ending_year = 2015;
  $count = 0;

  for ($x = $starting_year; $x <= $ending_year; $x++) {
    $count++;
  } 
  echo $count;
?>

答案 2 :(得分:1)

你不需要循环。

试试这个:

<?php
    $starting_year=2001;
    $ending_year=2015;
    $generation = $ending_year - $starting_year;
    echo $generation;
?>