• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / php range() – Create Array of Elements Within Given Range

php range() – Create Array of Elements Within Given Range

March 29, 2022 Leave a Comment

With the php range() function, we can create arrays of elements in a range of numbers and letters.

$arr = range(0,10);

print_r($arr);

//Output:
Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 5
    [6] => 6
    [7] => 7
    [8] => 8
    [9] => 9
    [10] => 10
)

Creating a range of letters is easy as well with the php range() function.

$arr = range("a","d");

print_r($arr);

//Output:
Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
)

You can also create a range that steps with an increment or decrement between elements in the sequence.

$arr = range(0,8,2);

print_r($arr);

//Output:
Array
(
    [0] => 0
    [2] => 2
    [4] => 4
    [6] => 6
    [8] => 8
)

When working in php, being able to easily create arrays of elements is very useful. One such case is when we want to generate an array of elements in a range of numbers or letters.

We can create an array of consecutive numbers or letters with the php range() function.

The range() function takes in 3 arguments. The first argument is the starting point, the second argument is the ending point, and the third argument is the step size.

range() returns an array of elements from the starting point to ending point, inclusive of both points.

For example, we can create an array with the numbers from 1 to 10 with php range() function.

$arr = range(0,10);

print_r($arr);

//Output:
Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 5
    [6] => 6
    [7] => 7
    [8] => 8
    [9] => 9
    [10] => 10
)

Using php range() Function to Create an Array with a Range of Letters

With range(), we can also create an array of letters in a range. Just like with numbers, we pass a starting point and an ending point.

range() will start at a particular letter and add elements until the ending letter.

Below is a simple example of creating an array of the letters from ‘a’ to ‘d’.

$arr = range("a","d");

print_r($arr);

//Output:
Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
)

Using range() to Create an Array with a Range of Numbers with Step

range() takes an optional third parameter called ‘step’ which allows us to increment the elements in the range by different numbers.

The default is to increment by 1, but if you want to step by 2 or more, then you can pass another number.

For example, if we want to create an array of even numbers in a range, we start with an even number and step by 2.

Below is an example in php for how to create an array of even numbers with range().

$arr = range(0,8,2);

print_r($arr);

//Output:
Array
(
    [0] => 0
    [1] => 2
    [2] => 4
    [3] => 6
    [4] => 8
)

If you want to create an array of odd numbers in a range, you can start with an odd number and step by 2.

Below is an example in php for how to create an array of odd numbers with range().

$arr = range(1,9,2);

print_r($arr);

//Output:
Array
(
    [0] => 1
    [1] => 3
    [2] => 5
    [3] => 7
    [4] => 9
)

Hopefully this article has been useful for you to use the php range() function to create an array of elements in a range given a starting and ending position.

Other Articles You'll Also Like:

  • 1.  Check if Variable Exists in php with isset() Function
  • 2.  Replace Space with Dash in php String
  • 3.  Truncate Strings in php Using substr()
  • 4.  php property_exists() – Check if Property Exists in Class or Object
  • 5.  php sleep() Function – Pause Execution of Code in php
  • 6.  Remove String from String in php with str_replace()
  • 7.  php asin – Find Arcsine and Inverse Sine of Number Using asin() Function
  • 8.  php Square Root with sqrt Function
  • 9.  Get Word Count of String in php with str_word_count() Function
  • 10.  PHP Numerically Indexed Array Begin With Position 0

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy