How to use range in PHP

In this article, I will explain how the range() function can be used in PHP.
  • 1406

range() function in PHP

  • The range() function is used to create an array containing a range of elements.

  • The range() function returns an array of elements from low to high.

Syntax

range(low,high,step)

Parameter

  • low low is required parameter. Determine the lowest value of the array.
  • high high is required parameter. Determine the highest value of the array.
  • step step is optional parameter. Determine the increment used in the array. Default value is 1.

Example

The following example show to how the range() function can be used in PHP.

<html>

<body>

<h3 style="color: Blue;">range() function example in PHP</h3>

    <?php

    echo"First range demo 1-10: ";

    foreach (range(0, 10) as $num) {

    echo "$num, ";

    }

    echo "<br />";

    echo "Second range demo : ";

    foreach (range(0, 100, 10) as $num) {

    echo "$num, ";

    }

    echo "<br />";

    echo "Third range demo : ";

    foreach (range('a', 'n') as $letter) {

    echo "$letter, ";

    }

    echo "<br />";

    echo "Fourth range demo : ";

    foreach (range('d', 'a') as $letter) {

    echo "$letter, ";

    }

    echo "<br />";

    ?>

</body>

</html>

 

Output

rangephp.jpg
You may also want to read these related articles here
 
Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.