burnside Posted May 8, 2008 Share Posted May 8, 2008 Hey all, Got a little problem, I have the code below : <?php foreach (range('0000','9999') as $num) { echo " $num <br />"; } ?> Work's but hasnt done what i expected it to do, what i want is for it to loop through : 0001 0002 0003 9998 9999 etc ... ... but it doesnt it starts at 1 and not 0001 any one got any ideas? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted May 8, 2008 Share Posted May 8, 2008 Range expects integers, so it will convert them. Just pad the 0s on when you print: <?php foreach(range(0,9999) as $num){ echo " ".str_pad($num,4,'0',STR_PAD_LEFT)." <br />"; } ?> Quote Link to comment Share on other sites More sharing options...
burnside Posted May 8, 2008 Author Share Posted May 8, 2008 Haha thanks that works great, is there a way to cound how many times it loped through? i would use count but it's not in a database haha Quote Link to comment Share on other sites More sharing options...
rhodesa Posted May 8, 2008 Share Posted May 8, 2008 Well, if it always starts at 0, it will be $num + 1 once it's done: <?php foreach(range(0,9999) as $num){ echo " ".str_pad($num,4,'0',STR_PAD_LEFT)." <br />"; } echo "It ran ".($num + 1)." times"; ?> if you don't know...just use a counter: <?php $i = 0; foreach(range(0,9999) as $num){ echo " ".str_pad($num,4,'0',STR_PAD_LEFT)." <br />"; $i++; } echo "It ran $i times"; ?> Quote Link to comment Share on other sites More sharing options...
burnside Posted May 8, 2008 Author Share Posted May 8, 2008 Thank you. TOPIC SOLVED 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.