range函數用于創建一個包含指定范圍內所有元素的數組。它的基本用法如下:
range(start, end, step)
示例:
$numbers = range(1, 10);
print_r($numbers);
// 輸出:Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 10 )
$letters = range('a', 'z');
print_r($letters);
// 輸出:Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f [6] => g [7] => h [8] => i [9] => j [10] => k [11] => l [12] => m [13] => n [14] => o [15] => p [16] => q [17] => r [18] => s [19] => t [20] => u [21] => v [22] => w [23] => x [24] => y [25] => z )
range函數還可以用于生成遞減的數組,只需將起始值設置為大于結束值的值即可。