PHP range() Function: Creating Arrays with Specified Elements

The PHP range() function generates an array of sequential values based on a start, end, and optional step, allowing numeric and character sequences, with examples demonstrating default steps, custom steps, and reverse ordering.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP range() Function: Creating Arrays with Specified Elements

The PHP range() function creates an array containing a sequence of elements defined by a start value, an end value, and an optional step.

Parameters: start – the first value of the sequence. end – the value at which the sequence stops (inclusive). step – optional increment between elements; must be positive, defaults to 1.

Return value: an array of values from start to end, inclusive.

Examples:

<?php
// Numeric range from 0 to 12
foreach (range(0, 12) as $number) {
    echo $number;
}

// Range with step of 10 from 0 to 100
foreach (range(0, 100, 10) as $number) {
    echo $number;
}

// Character range from 'a' to 'i'
foreach (range('a', 'i') as $letter) {
    echo $letter;
}

// Reverse character range from 'c' to 'a'
foreach (range('c', 'a') as $letter) {
    echo $letter;
}
?>
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendArrayfunctionexamplerange
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.