PHP range Function | Create Array With Range of Elements

PHP range is an inbuilt Function in PHP. It creates an array containing a range of elements of any kind such as integer or alphabets. It creates an array within the input range (from low to high). In this article, we will discuss the PHP range Function. Also, we will discuss a few examples to demonstrate it’s usage.

Syntax

array range(low, high, step = 1)

Parameters

The PHP range Function expects three parameters. Two parameters are mandatory, while the third one is optional.

  • $low: It is the first value in the array you need. It is mandatory to pass this parameter.
  • $high: It refers to the last value in the array you require. Also, it is a mandatory parameter.
  • $step: It refers to the increment size you need in the array. It is optional to pass with the default value as 1.

Return Value

The PHP range Function returns an array of elements from low to high. Also, the step size of the array can be changed by passing the step parameter.

PHP range Function Diagram

Examples

Let’s discuss a few examples of using range Function

Example 1: Integer Array

<?php

	$testArray = range(1, 5);
	print_r($testArray);
	/*
	Array
	(
	    [0] => 1
	    [1] => 2
	    [2] => 3
	    [3] => 4
	    [4] => 5
	)
	*/

?>

In the above article, the function returns an array from 1 to 5. This is because, the $low parameter is set to 1 and the $high parameter is set to 5. Since, the $step parameter is not passed, the default value 1 is used.

Example 2: Custom Step Size

You can set a custom step size by passing the $step parameter.

<?php

	$testArray = range(1, 10, 2);
	print_r($testArray);
	/*
	Array
	(
	    [0] => 1
	    [1] => 3
	    [2] => 5
	    [3] => 7
	    [4] => 9
	)
	*/

?>

In the above example, the $low parameter is 1 and the $high parameter is 10. However, the $step parameter is 2 and the function returns an array from 1 to 9 with increment size as 2.

Example 3: Reverse Array

Also, you can create an array with reverse order using PHP range Function.

<?php

	$testArray = range(5, 1);
	print_r($testArray);
	/*
	Array
	(
	    [0] => 5
	    [1] => 4
	    [2] => 3
	    [3] => 2
	    [4] => 1
	)
	*/

?>

Example 4: Alphabets Array

You can also pass alphabets in $low and $high parameter. The function returns an array with alphabets.

<?php

	$testArray = range('a', 'e');
	print_r($testArray);
	/*
	Array
	(
	    [0] => a
	    [1] => b
	    [2] => c
	    [3] => d
	    [4] => e
	)
	*/

?>

Example 5: Reverse Alphabets with Step Size

Similarly, you can pass step size and get alphabets in reverse order.

<?php

	$testArray = range('m', 'c', 2);
	print_r($testArray);
	/*
	Array
	(
	    [0] => m
	    [1] => k
	    [2] => i
	    [3] => g
	    [4] => e
	    [5] => c
	)
	*/

?>

Conclusion

In this article, we discussed the PHP range Function. You can read more about it on the Official Documentation of PHP. Also, you can read about more PHP Array Functions on Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *