TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-28-2007, 11:57 AM   #1 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 1,654
Thanks: 73
Wildhoney is on a distinguished road
Smile Generating a Range of Numbers the Easy Way

After Haris' mention of the PHP function range(), I thought the kindest thing to do would be to give it a proper introduction. So here is that introduction!

Range is native PHP function that takes 2 compulsory arguments and 1 optional argument (As of PHP 5.0.0 which defaults to 1) and returns an array. Seem ever so simple? Well it is! The 3 arguments the function can accept are as follows:
  • $low: The number or character to begin at.
  • $high: The number or character to end at.
  • $step: The amount to count up or down in.

Take the example below which will begin at 0, increment in steps of 10 until we get to 50.

PHP Code:
foreach(range(05010) as $iNumber)
{
    echo 
$iNumber ' ';

This would result in 0 10 20 30 40 50 being returned. If you were to assign the result of the range() into an array then all you would see is a nicely constructed array:

Code:
Array
(
    [0] => 0
    [1] => 10
    [2] => 20
    [3] => 30
    [4] => 40
    [5] => 50
)
The great thing about this function is that it can also count up or down in characters. For instance, if we wanted to list the 6 characters from A to F, then we could do that quite easily with range():

PHP Code:
$aArray range('A''F');
echo 
implode(', '$aArray); 
Which would output A, B, C, D, E, F. Range can also go backwards, which can be a whole lot of fun. Not quite as exciting as a rollercoaster going backwards but I hope you appreciate me trying to liven the article up!

PHP Code:
$aArray range(100025);
echo 
implode(', '$aArray); 
As I'm sure you can surmise, the above example would give us 100, 75, 50, 25, 0!

There is not a lot more to it. Such an easy function but one that is perhaps not as well heard of as it should be. I can't say that I've really needed this function before, however, perhaps there might be an occasion where you would require its presence! I'd be interested to know if anyone out there has any really good uses for it? I've seen some good uses on php.net for such tasks as checking if an array is associative - but any more would be great to hear!
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-24-2007, 12:18 PM   #2 (permalink)
The Acquainted
 
Join Date: Oct 2007
Posts: 131
Thanks: 12
maZtah is an unknown quantity at this point
Default

Good article/introduction.

What's the advantage of using range() instead of just a for loop?
maZtah is offline  
Reply With Quote
Old 11-24-2007, 06:12 PM   #3 (permalink)
The Wanderer
 
Join Date: Nov 2007
Posts: 12
Thanks: 0
DarkPrince11 is on a distinguished road
Default

Range creates an array of the numbers without and has less overhead than calling a loop. It's basically quicker, but if you're planning on printing the numbers, than it's better to use a for loop, because you'll eventually have to loop the range array to print it out.
Send a message via AIM to DarkPrince11 Send a message via MSN to DarkPrince11
DarkPrince11 is offline  
Reply With Quote
Old 11-25-2007, 12:42 AM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 1,654
Thanks: 73
Wildhoney is on a distinguished road
Default

I must admit that I've never used the range function, despite being fully aware of it. I suppose the only instance I could think of using it, is in an array:

php Code:
foreach(range(1, 10) as $iNumber)
{

}

What other purposes are there?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-25-2007, 01:09 AM   #5 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 356
Thanks: 24
Haris is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
I must admit that I've never used the range function, despite being fully aware of it. I suppose the only instance I could think of using it, is in an array:

php Code:
foreach(range(1, 10) as $iNumber)
{

}

What other purposes are there?
Populating fields is only what comes into my mind.
Haris is offline  
Reply With Quote
Old 11-25-2007, 09:03 PM   #6 (permalink)
The Acquainted
 
wGEric's Avatar
 
Join Date: Nov 2007
Posts: 125
Thanks: 0
wGEric is on a distinguished road
Default

It has it's uses. I've populated arrays with A-Z using range. Easier than typing out every letter.
__________________
Eric
BlogphpBB
wGEric is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 06:47 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0