整数数组


arrays of integers

我有一个数组:

$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");

在我的html中,我将数组中的值作为下拉列表:

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

我想知道如何生成一个数字范围的数组,以便在未来轻松更改它,而不是手动键入所有数字?我能做这样的事吗?

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

当我做上述操作时,我得到了未定义的推送函数错误

在PHP中,数组没有任何函数。您将使用array_push

然而,PHP有一个内置的:range

$age = range(18, 80);