Jump to content

How do I make php count from 000 to 999?


bobleny

Recommended Posts

Rather than make a new topic and clutter the forum with my stupid php noob questions, I posted another question below

How 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>
Link to comment
https://forums.phpfreaks.com/topic/13478-how-do-i-make-php-count-from-000-to-999/
Share on other sites

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 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 below

How 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...
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 :)
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 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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.