monkeytooth Posted January 28, 2008 Share Posted January 28, 2008 Now this may be a very stupid question with a really simple answer, and I have probably been staring at the answer for awhile now.. but I'm going to ask anyway.. I need to generate a list of numbers, well say 1-100, which isn't to hard I know, with a while loop, but heres the part im kicking myself over with.. I dont want 1,2,3,4,5,6,7,8,9,10.. I want ouput like 001,002,003,004,005,006,007,008,009,010.. and so on to 100 and in some cases more.. but Im assuming if it can be done one way a slight alteration can be made to go higher.. anyway heres a pic sample.. so what do you think possible? im almost sure it is,but for the life of me I can't get it right.. Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted January 28, 2008 Share Posted January 28, 2008 use str_pad... for ($i = 0; $i < 1000; $i++) { echo str_pad($i, 3, "0", STR_PAD_LEFT); } Quote Link to comment Share on other sites More sharing options...
monkeytooth Posted January 28, 2008 Author Share Posted January 28, 2008 will try that.. thank you, don't think I've ever come across that function so that helps.. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted January 28, 2008 Share Posted January 28, 2008 Using printf() is easier: <?php for ($i = 0;$i < 1000; $i++) printf("%03d ",$i); ?> Ken Quote Link to comment Share on other sites More sharing options...
monkeytooth Posted January 28, 2008 Author Share Posted January 28, 2008 I have another question, this one im not to sure about it though.. Is it possible to take an input of any kind (number only). And have it get the highest count for that total ammount of numbers.. If that makes sence. Ok lets say I want to go from 1-9999 with the above function. But all I want to do is input the number 4 and work something up that can generate the highest number possible with 4 digits: 9999. Or I want to pop the number 2 in the input and have it go come up with 99, or 3 and have it figure out that 3 numbers is 999.. I think in typing this out I have come up with my own solution to this particular answer but Ill keep the question and post the concept to find out if you all think its doable.. echo str_pad($i, 3, "9", STR_PAD_LEFT); where I turn the 3 in the string to a var that will take my input and do as i wish.. In fact Im just gonna go try that now.. but if someones got a better idea that that I am for it Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted January 29, 2008 Share Posted January 29, 2008 Use the pow() function <?php $places = 4; echo pow(10,$places) - 1 . "<br>"; ?> This will print 9999 Ken Quote Link to comment Share on other sites More sharing options...
monkeytooth Posted January 29, 2008 Author Share Posted January 29, 2008 Sweet, thanks for all the help.. now I can seriously move on with the overall part of the script.. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.