Current Location: Home> Latest Articles> How to Generate a Descending Array with the range() Function in PHP: Easy Tips to Master It

How to Generate a Descending Array with the range() Function in PHP: Easy Tips to Master It

gitbox 2025-09-12

<?php
/*

  • Title: How to Generate a Descending Array with the range() Function in PHP: Easy Tips to Master It

  • The range() function in PHP is a very useful function for creating arrays. It can quickly generate a sequence of numbers or characters.

  • In most cases, using range() by default creates an ascending array, such as from 1 to 10.

  • But what if you want to generate a descending array? This article will explain in detail the methods and tips for generating descending arrays with the range() function.
    */

echo "

What is the range() Function?

";
echo "

The basic syntax of the range() function is:

"
;
echo "
range(mixed $start, mixed $end, int $step = 1): array
"
;
echo "

Here, $start is the starting value, $end is the ending value, and $step is the step, which defaults to 1.

"
;

echo "

Example of Default Ascending Array

"
;
echo "
"</span></span><span>;<br>
</span><span><span class="function_ invoke__">print_r</span></span><span>(</span><span><span class="function_ invoke__">range</span></span><span>(</span><span><span>1</span></span><span>, </span><span><span>5</span></span><span>));<br>
</span><span><span>echo</span></span><span> </span><span><span>"
"
;
echo "

Output: [1, 2, 3, 4, 5]

"
;

echo "

How to Generate a Descending Array?

"
;
echo "

To create a descending array, set the starting value higher than the ending value and use a negative step.

"
;

echo "

Example Code:

"
;
echo "
"</span></span><span>;<br>
</span><span><span>echo</span></span><span> </span><span><span>"$arr = range(10, 1, -1);\n"</span></span><span>;<br>
</span><span><span class="function_ invoke__">print_r</span></span><span>(</span><span><span class="function_ invoke__">range</span></span><span>(</span><span><span>10</span></span><span>, </span><span><span>1</span></span><span>, -</span><span><span>1</span></span><span>));<br>
</span><span><span>echo</span></span><span> </span><span><span>"
"
;

echo "

Notes and Tips

"
;
echo "
    ";
    echo "
  • The step must be negative; otherwise, range() will return an empty array.
  • "
    ;
    echo "
  • The starting value should be greater than the ending value to create a descending sequence.
  • "
    ;
    echo "
  • The absolute value of the step determines the interval between elements; for example, -2 decreases by 2 units.
  • "
    ;
    echo "
"
;

echo "

More Examples

"
;

echo "

Generate a Descending Array of Odd Numbers:

"
;
echo "
"</span></span><span>;<br>
</span><span><span class="function_ invoke__">print_r</span></span><span>(</span><span><span class="function_ invoke__">range</span></span><span>(</span><span><span>19</span></span><span>, </span><span><span>1</span></span><span>, -</span><span><span>2</span></span><span>));<br>
</span><span><span>echo</span></span><span> </span><span><span>"
"
;

echo "

Generate a Descending Array of Letters:

"
;
echo "
"</span></span><span>;<br>
</span><span><span class="function_ invoke__">print_r</span></span><span>(</span><span><span class="function_ invoke__">range</span></span><span>(</span><span><span>'z'</span></span><span>, </span><span><span>'v'</span></span><span>, -</span><span><span>1</span></span><span>));<br>
</span><span><span>echo</span></span><span> </span><span><span>"
"
;

echo "

Conclusion

"
;
echo "

By properly setting the starting value, ending value, and negative step in the range() function, you can easily create descending arrays of numbers or letters. Mastering these techniques makes array generation in PHP more flexible and efficient.

"
;
?>