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.
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;
}
?>Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.