bobleny Posted July 2, 2006 Share Posted July 2, 2006 Rather than make a new topic and clutter the forum with my stupid php noob questions, I posted another question belowHow do I make php count from 000 to 999?This is what I have and there has got to be a way to do this with out three while loops![code]$i = "0";while ($i <= "9"){echo "00".$i."<br />\n";$i++;}$i = "10";while ($i <= "99"){echo "0".$i."<br />\n";$i++;}$i = "100";while ($i <= "999"){echo .$i.”<br />\n";$i++;}[/code]<previously>How do I make a while loop script vertically?Of course it displays correctly because html doesn't care if it is horizontal or vertical. I want it to script vertically so I can analyze the source code online.</previously> Quote Link to comment https://forums.phpfreaks.com/topic/13478-how-do-i-make-php-count-from-000-to-999/ Share on other sites More sharing options...
wildteen88 Posted July 2, 2006 Share Posted July 2, 2006 Add the newline character at the end of the line, like so:[code=php:0]$i = 1;while($i <= 10){ echo "This has looped {$i} times<br />\n"; $i++;}[/code]\n is the newline chacter. Also in order for \n to work it must be used within double quotes and not single quotes. Quote Link to comment https://forums.phpfreaks.com/topic/13478-how-do-i-make-php-count-from-000-to-999/#findComment-52123 Share on other sites More sharing options...
bobleny Posted July 2, 2006 Author Share Posted July 2, 2006 Ah, I hade thought about doing that but I was thinking it would do the same as “<br />“. O.K.Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/13478-how-do-i-make-php-count-from-000-to-999/#findComment-52128 Share on other sites More sharing options...
wildteen88 Posted July 2, 2006 Share Posted July 2, 2006 < br /> and \n are completly different. The < br /> tag is used to tell the browser to add a new line to the web page, whereas \n tells the operating system to add a new line to the source file. Quote Link to comment https://forums.phpfreaks.com/topic/13478-how-do-i-make-php-count-from-000-to-999/#findComment-52130 Share on other sites More sharing options...
bobleny Posted July 2, 2006 Author Share Posted July 2, 2006 How do I make php count from 000 to 999? Quote Link to comment https://forums.phpfreaks.com/topic/13478-how-do-i-make-php-count-from-000-to-999/#findComment-52164 Share on other sites More sharing options...
corbin Posted July 2, 2006 Share Posted July 2, 2006 [quote author=bobleny link=topic=99213.msg390622#msg390622 date=1151865060]Rather than make a new topic and clutter the forum with my stupid php noob questions, I posted another question belowHow do I make php count from 000 to 999?This is what I have and there has got to be a way to do this with out three while loops![code]$i = "0";while ($i <= "9"){echo "00".$i."<br />\n";$i++;}$i = "10";while ($i <= "99"){echo "0".$i."<br />\n";$i++;}$i = "100";while ($i <= "999"){echo .$i.”<br />\n";$i++;}[/code]<previously>How do I make a while loop script vertically?Of course it displays correctly because html doesn't care if it is horizontal or vertical. I want it to script vertically so I can analyze the source code online.</previously>[/quote]Try [code]$i = "0";while ($i <= "100"){if($i < 10) { echo "0"; }if($i < 100) { echo "0"; }echo $i<br />\n";$i++;}[/code]Lets say it passed through 9 as $i... It would be less than 10 and less than 100 so it would add 2 0's making it 009, but lets say you pass 23 through it would be > 10 but less than 100 so it would add once 0 making it 023... Quote Link to comment https://forums.phpfreaks.com/topic/13478-how-do-i-make-php-count-from-000-to-999/#findComment-52167 Share on other sites More sharing options...
Koobi Posted July 2, 2006 Share Posted July 2, 2006 you really should have created a new thread since it's a different topic.please do so the next time.try using [url=http://www.php.net/str_pad]str_pad()[/url][code=php:0]<?php for($count = 0; $count <= 999; $count++) { echo str_pad($count, 3, '0', STR_PAD_LEFT) . "<br />\r\n"; }?>[/code]i recommend using \r\n instead of just \n for cross platform compatibility.also, i believe there was a more efficient solution for this sort of thing (padding numbers) but i can't remember what it was. hope this suffices for now :) Quote Link to comment https://forums.phpfreaks.com/topic/13478-how-do-i-make-php-count-from-000-to-999/#findComment-52169 Share on other sites More sharing options...
bobleny Posted July 2, 2006 Author Share Posted July 2, 2006 That works great thanks!Also, I would have started a new topic but I knew it would be an easy quick answer with no debate. Quote Link to comment https://forums.phpfreaks.com/topic/13478-how-do-i-make-php-count-from-000-to-999/#findComment-52185 Share on other sites More sharing options...
Koobi Posted July 2, 2006 Share Posted July 2, 2006 glad it worked. do you understand how it works though? it's mainly thanks to the str_pad() function.in the future try and make a new thread for a new topic because it would benefit users after you who might search for a similar problem :) Quote Link to comment https://forums.phpfreaks.com/topic/13478-how-do-i-make-php-count-from-000-to-999/#findComment-52188 Share on other sites More sharing options...
bobleny Posted July 2, 2006 Author Share Posted July 2, 2006 [quote author=Koobi link=topic=99213.msg390692#msg390692 date=1151870800]glad it worked. do you understand how it works though? it's mainly thanks to the str_pad() function.in the future try and make a new thread for a new topic because it would benefit users after you who might search for a similar problem :)[/quote]Point taken! I will start new topics.Yup, I understand it. I looked it up on php.net. I just never herd of the function before. I know very few functions.Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/13478-how-do-i-make-php-count-from-000-to-999/#findComment-52192 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.