homepage Welcome to WebmasterWorld Guest from 54.161.247.22
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
How do you create a list of numbers?
internetheaven




msg:4625636
 1:36 pm on Nov 24, 2013 (gmt 0)

This is for a PHP program I have already written. I don't need to do this in PHP, I just need the list of numbers.

I need to create some lists of numbers.

e.g. the numbers 1-10,000 in 50s (1, 51, 101, 151 etc.)

the numbers 10-20,000 in tens (10, 20, 30, 40 etc.)

How can you do that? All I can find is ways to make sequential numbers (1, 2, 3, 4 etc.)

Help!

Thanks
Mike

 

penders




msg:4625679
 7:09 pm on Nov 24, 2013 (gmt 0)

$min = 1; $max = 10000; $step = 50; 
for ($i = $min; $i <= $max; $i += $step) {
echo $i;
}


Is the formatting of this list specific?

lucy24




msg:4625701
 9:06 pm on Nov 24, 2013 (gmt 0)

the numbers 1-10,000 in 50s (1, 51, 101, 151 etc.)

the numbers 10-20,000 in tens (10, 20, 30, 40 etc.)

Will you require the php to read your mind and know that "in 50s" = 50x + 1 beginning with x = 0, while "in tens" = 10x beginning with x = 1? Or will you be able to say this part explicitly?

JD_Toims




msg:4626632
 11:29 pm on Nov 28, 2013 (gmt 0)

Just a little code shortening trick I thought I'd add.

The for() above can be coded like this to get working:

for(
$min=1,
$max=10000,
$step=50,
$i=$min;

$i<=$max;

print $i.PHP_EOL,
$i+=$step
);

Then it can be "put away" on a single line when it's done:

for($min=1,$max=10000,$step=50,$i=$min;$i<=$max;print $i.PHP_EOL,$i+=$step);



BTW: You have to use print rather than echo when coding the for() in this style, because echo will accept multiple arguments, but print will only accept one argument so "multiples" for output must be concatenated or quoted.

EG

print 'here',PHP_EOL; // Won't Work
print 'here'.PHP_EOL; // Works

echo 'here',PHP_EOL; // Works
echo 'here'.PHP_EOL; // Works
echo $i=0,' here ',$i+=50,PHP_EOL; // Works

Readie




msg:4626918
 6:48 pm on Nov 30, 2013 (gmt 0)

There's a much simpler way than for loops if you wish to build the list as an array for other purposes

$range = range(0, 10000, 50); // The numbers 0, 50, 100... 10000



I would also like to comment on JD_Toims' 1 liner - I can appreciate it's very neat, but while it may save a little screen space and a few bytes; minifying the code in such a way sort of obsfucates it, and makes it more difficult to read.

JD_Toims




msg:4626962
 2:29 am on Dec 1, 2013 (gmt 0)

I can appreciate it's very neat, but while it may save a little screen space and a few bytes; minifying the code in such a way sort of obsfucates it, and makes it more difficult to read.

It's not about saving a few bytes. It's about often having so many lines of code "jumping back and forth" from an initial block to another where there are blocks in between can require multiple page-ups/downs which could be eliminated so blocks "above" and "below" can be viewed at the same time, since those not needing to be read every single time aren't in the way if they're "collapsed".



It took a couple years of coding before I realized the whole "expanded code all the time" theory [which is often "touted as great style"] wasted more of my time by forcing me to scroll pages than "collapsing" the code I had working then "expanding" if necessary did -- Of course, I use space or single-space tab indenting rather than four-space tabs, because I can get more code on a single page view that way too, so I'm probably "not a very good coder" in the opinion of many.

Now TextWrangler does the collapsing for me, but there are those who might not have the same ability to use TextWrangler, and I thought I'd give them an idea. Also, honestly, if someone cannot figure out how to "expand" what they need to be more readable when they need it to so they can work on it, then they may not be qualified to work on most of the code I write in the first place.



It's very simple to "go the other way" and make the example I gave more readable again -- All someone has to do is start at the "close", press return/enter and then return/enter once if they find a , and twice if they find a ; in the line, like this:

for($min=1,$max=10000,$step=50,$i=$min;$i<=$max;print $i.PHP_EOL,$i+=$step);

>

for($min=1,$max=10000,$step=50,$i=$min;$i<=$max;print $i.PHP_EOL,$i+=$step
);

>>

for($min=1,$max=10000,$step=50,$i=$min;$i<=$max;print $i.PHP_EOL,
$i+=$step
);

>>>

for($min=1,$max=10000,$step=50,$i=$min;$i<=$max;

print $i.PHP_EOL,
$i+=$step
);

>>>>

for($min=1,$max=10000,$step=50,$i=$min;

$i<=$max;

print $i.PHP_EOL,
$i+=$step
);

>>>>>

Etc.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved