How to use php range function

青灯夜游
Release: 2023-02-22 21:18:01
Original
2665 people have browsed it

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)
Copy after login

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);
?>
Copy after login

Output:

Array ( [0] => b [1] => c [2] => d [3] => e )
Copy after login

Example 2:

<?php
$number = range(1,10,2);
print_r ($number);
?>
Copy after login

Output:

Array ( [0] => 1 [1] => 3 [2] => 5 [3] => 7 [4] => 9 )
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!