从一个var计数到另一个var

Hey guys a quick question in php.

This probably is pretty simple I'm not sure but I would like to know how to count up from one variable to another. So say I have the value 5 and the value 9 in the other I would like the PHP code to count each number and store in a array.

e.g

$var1 = 5;

$var2 = 9;

it should store each value (5,6,7,8,9) in a array so i can access them like so arrayname[1]

Thanks in advance

This should do it for you

$var1 = 5;
$var2 = 9;

foreach (range($var1, $var2) as $number) {
  echo $number;
}

If you want to store it as an array then

$number  = range($var1, $var2);

Another alternative:

$var1 = 5;
$var2 = 9;
$numbers = array();
for ( $i=$var1; $i <= $var2; $i++ ) {
    $numbers[] = $i;
}

Then the variable $numbers should be the array that you want.