Home  >  Article  >  Backend Development  >  How to use php range function

How to use php range function

青灯夜游
青灯夜游Original
2019-05-28 14:38:032678browse

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 php range function

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(&#39;b&#39;,&#39;e&#39;);
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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn