The range() function is a built-in function in PHP, used to create an array of any type containing elements in a specified range. The syntax is: range(low,high,step), which returns an array containing integers or characters from low to high (including low and high).
How to use the php range() function?
php range() function creates an array containing elements in a specified range
Syntax:
range(low,high,step)
Parameters: This function accepts three parameters
● low: required. Specifies the minimum value of the array.
● High: required. Specifies the maximum value of the array.
● Step: Optional. Specifies the stepping between elements. The default is 1.
Return value: This function creates an array containing integers or characters from low to high (including low and high). If high is smaller than low, return the array in reverse order.
Let’s take a look at how to use the php range() function through an example.
Example 1:
<?php $str = range('b','e'); print_r ($str); ?>
Output:
Array ( [0] => b [1] => c [2] => d [3] => e )
Example 2:
<?php $number = range(1,10,2); print_r ($number); ?>
Output:
Array ( [0] => 1 [1] => 3 [2] => 5 [3] => 7 [4] => 9 )
The above is the detailed content of How to use php range function. For more information, please follow other related articles on the PHP Chinese website!