PHP:为范围创建一个数组

2022-01-17 00:00:00 arrays numbers variables range php

如果我有一个变量 $num = 50 如何将数字 1-50 放入数组中?

If I have a variable $num = 50 how can I put the numbers 1-50 into an array?

推荐答案

这可以通过一个简单的for 循环:

This can be solved by using a simple for loop:

//  Start ↓    End ↓  Step ↓
for ($i = 1; $i <= $num; ++$i) {
    $array[] = $i;
}

相关文章