TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   List of numbers (http://www.talkphp.com/absolute-beginners/1469-list-numbers.html)

paulOr 11-15-2007 01:06 PM

List of numbers
 
im wanting to desplay a list of numbers like:
Code:

1
2
3
4
5
6
7
8
9
10

is there such code that will allow me to list numbers like this, but with my own preset bits like numbers between: 555444 - 555555 would show:
Code:

555445
555446
555447
555448
555449
555450

and so on

thanks in advance

Karl 11-15-2007 01:16 PM

Hey paulOr, try this:

PHP Code:

<?php
$aRange 
range(5554455554501);

foreach (
$aRange as $iValue)
{
    echo 
$iValue '<br />';    
}
?>


paulOr 11-15-2007 01:19 PM

perfect! <3 thanks alot

Tanax 11-15-2007 01:31 PM

What's the arguement "1" doing??

Salathe 11-15-2007 01:43 PM

The third argument to range is for the step. In other words, how much the resulting numbers should differ by. For example, range(1, 20, 3) would result in an array with values 1, 4, 7, 10, 13, 16 and 19.

Wildhoney 11-15-2007 01:43 PM

The 1 in the range specifies how much to step up by on each increment (or decrement). So if it was set to 2 then we would have:
  • 555445
  • 555447
  • 555449
  • etc...

(Salathe beat me to it but I'll keep this in anyway :)

Tanax 11-15-2007 04:03 PM

Ahhh!
Thanks for the heads up! :D

wGEric 11-15-2007 08:55 PM

You could do a simple for loop as well.

PHP Code:

for($i 555444$i <= 555555$i++)
{
    echo 
$i '<br />';



Wildhoney 11-22-2007 02:57 PM

I wrote a nice article about range over here. No doubt worth a read!

ReSpawN 11-23-2007 08:49 PM

Quote:

Originally Posted by wGEric (Post 4231)
You could do a simple for loop as well.

PHP Code:

for($i 555444$i <= 555555$i++)
{
    echo 
$i '<br />';



The one way, the only was, to make it easy to remember if you ask me. I am used to it since I make a lot of these loops, but you can also use while loops, instead of for and foreach. :)

Wildhoney 11-25-2007 12:44 AM

A similar discussion going on in another part of TalkPHP! I'd always use the for loop as well, to be fair. I don't see the range function as having any other uses apart from that which can be achieved using the for loop. I imagine the range function itself contains nothing more than a for loop.


All times are GMT. The time now is 09:00 PM.

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