beansandsausages 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? Link to comment https://forums.phpfreaks.com/topic/104734-solved-loop/ 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 />"; } ?> Link to comment https://forums.phpfreaks.com/topic/104734-solved-loop/#findComment-536093 Share on other sites More sharing options...
beansandsausages 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 Link to comment https://forums.phpfreaks.com/topic/104734-solved-loop/#findComment-536096 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"; ?> Link to comment https://forums.phpfreaks.com/topic/104734-solved-loop/#findComment-536102 Share on other sites More sharing options...
beansandsausages Posted May 8, 2008 Author Share Posted May 8, 2008 Thank you. TOPIC SOLVED Link to comment https://forums.phpfreaks.com/topic/104734-solved-loop/#findComment-536108 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.