PHP is a very popular programming language, and developers benefit from its many built-in functions and elements. Among them, the range() function is a very useful function built into PHP, which is used to create a regular array.
This article will introduce the usage of the range() function, including the syntax, parameters, return values and examples of the function. If you want to use the range() function to construct an array, this article will be very helpful to you.
The syntax of the range() function
The syntax of the range() function is:
range ($start, $end, $step);
Among them, $start represents the starting value of the array, $end represents the end of the array Value, $step represents the interval between each element in the array. These parameters must be of numeric type. If the $step parameter is not provided, it defaults to 1.
Note: In PHP, you can use integers or floating point numbers as parameters, but floating point numbers are rarely used as parameters.
Parameters of the range() function
The following is the parameter description of the range() function:
The return value of the range() function
The range() function will return an array, an arithmetic array from the starting value to the ending value, and the difference between its elements The value is determined by the third parameter $step. If $step is not specified, it will default to 1.
Examples of range() function
Here are some examples of using the range() function to create arrays:
$num_array = range(0, 10); print_r($num_array);
Output result:
Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 [10] => 10 )
$num_array = range(0, 10, 0.5); print_r($num_array);
Output result:
Array ( [0] => 0 [1] => 0.5 [2] => 1 [3] => 1.5 [4] => 2 [5] => 2.5 [6] => 3 [7] => 3.5 [8] => 4 [9] => 4.5 [10] => 5 [11] => 5.5 [12] => 6 [13] => 6.5 [14] => 7 [15] => 7.5 [16] => 8 [17] => 8.5 [18] => 9 [19] => 9.5 [20] => 10 )
$num_array = range(10, 0, -1); print_r($num_array);
Output result:
Array ( [0] => 10 [1] => 9 [2] => 8 [3] => 7 [4] => 6 [5] => 5 [6] => 4 [7] => 3 [8] => 2 [9] => 1 [10] => 0 )
Summary
This article introduces the usage of PHP's built-in function range(), including the function's syntax, parameters, and return value and examples. By using the range() function, you can easily create an array containing regular elements. If you want to learn more about PHP's arrays and functions, you can learn from the official documentation or other tutorials.
The above is the detailed content of Introduction to the usage of PHP range() function. For more information, please follow other related articles on the PHP Chinese website!