Sunday, November 1, 2020

PHP | range() Function

PHP range() Function


Hello dear readers! welcome back to another section of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP range() Function.

PHP range() function returns an array of elements from low to high, inclusive. If the low parameter is greater than high parameter, the sequence will be from high to low.

Syntax

Following below is the syntax to use this function -

range ( $low, $high [, $step] );


Parameter Details

Sr.NoParameter & Description
1

low(Required)

This is a lower range of the array

2

high(Required)

This is a upper range of the array

3

step(Optional)

Steps to increase array element. By default it is 1


Return Value

This function returns an array of elements.

Example

Try out the below example -

<?php
   foreach (range(0, 6) as $number) {
      echo "$number, ";
   }
   print "\n";
   
   foreach (range(0, 100, 10) as $number) {
      echo "$number, ";
   }
   print "\n";
   
   foreach (range('a', 'c') as $letter) {
      echo "$letter, ";
   }
   print "\n";
   
   foreach (range('f', 'a') as $letter) {
      echo "$letter, ";
   }
   print "\n";
?> 

Output

When the above code is executed, it will produce the following result -

0, 1, 2, 3, 4, 5, 6,
0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
a, b, c,
f,e,d,c, b, a


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP reset() Function.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.