rory-h rory-h - 3 months ago 10
PHP Question

arrays of integers

I have an array:

$age = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31");


and in my html I'm getting the values in the array to be a in dropdown list:

foreach($age as $val)
{echo '<option value="'.$val.'">'.$val.'</option>'; }?>


was wondering how can I generate an array of range of numbers so I can easily change it in the future instead of manual keying all the numbers?
can I do something like this?

$minNum = 18;
$maxNum = 80;
$age = [];
while($minNum <= $maxNum){
$age.push($minNum++);
}


when I do the above I got undefined push function error

Answer

In PHP, arrays don't have any functions. You would use array_push.

However, PHP has a built-in for this: range

$age = range(1, 31);
Comments