Jump to content


Photo

How do I make php count from 000 to 999?


  • Please log in to reply
9 replies to this topic

#1 bobleny

bobleny
  • Members
  • PipPipPip
  • Advanced Member
  • 429 posts

Posted 02 July 2006 - 06:31 PM

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!

$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++;
}





<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>
-- www.firemelt.net --
First do me a favor and read this: JavaScript is NOT Java - Then read this: www.php.net - When your done with that, read this Topic
After that, floors open. I and anyone else will be MORE than happy to answer YOUR query! [Topic Solved]
Cheer up, the worst has yet to come...

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 02 July 2006 - 06:34 PM

Add the newline character at the end of the line, like so:
$i = 1;
while($i <= 10)
{
    echo "This has looped {$i} times<br />\n";

    $i++;
}

\n is the newline chacter. Also in order for \n to work it must be used within double quotes and not single quotes.

#3 bobleny

bobleny
  • Members
  • PipPipPip
  • Advanced Member
  • 429 posts

Posted 02 July 2006 - 06:36 PM

Ah, I hade thought about doing that but I was thinking it would do the same as “<br />“. O.K.

Thanks!

-- www.firemelt.net --
First do me a favor and read this: JavaScript is NOT Java - Then read this: www.php.net - When your done with that, read this Topic
After that, floors open. I and anyone else will be MORE than happy to answer YOUR query! [Topic Solved]
Cheer up, the worst has yet to come...

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 02 July 2006 - 06:39 PM

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

#5 bobleny

bobleny
  • Members
  • PipPipPip
  • Advanced Member
  • 429 posts

Posted 02 July 2006 - 07:34 PM

How do I make php count from 000 to 999?
-- www.firemelt.net --
First do me a favor and read this: JavaScript is NOT Java - Then read this: www.php.net - When your done with that, read this Topic
After that, floors open. I and anyone else will be MORE than happy to answer YOUR query! [Topic Solved]
Cheer up, the worst has yet to come...

#6 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 02 July 2006 - 07:42 PM

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!

$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++;
}





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


Try
$i = "0";
while ($i <= "100")
{
if($i < 10) { echo "0"; }
if($i < 100) { echo "0"; }
echo $i<br />\n";
$i++;
}

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...
Why doesn't anyone ever say hi, hey, or whad up world?

#7 Koobi

Koobi
  • Staff Alumni
  • Advanced Member
  • 419 posts
  • LocationColombo, Sri Lanka | South Asia

Posted 02 July 2006 - 07:44 PM

you really should have created a new thread since it's a different topic.
please do so the next time.


try using str_pad()


<?php
	for($count = 0; $count <= 999; $count++)
	{
		echo str_pad($count, 3, '0', STR_PAD_LEFT) . "<br />\r\n";
	}
?>

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 :)

#8 bobleny

bobleny
  • Members
  • PipPipPip
  • Advanced Member
  • 429 posts

Posted 02 July 2006 - 08:04 PM

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.
-- www.firemelt.net --
First do me a favor and read this: JavaScript is NOT Java - Then read this: www.php.net - When your done with that, read this Topic
After that, floors open. I and anyone else will be MORE than happy to answer YOUR query! [Topic Solved]
Cheer up, the worst has yet to come...

#9 Koobi

Koobi
  • Staff Alumni
  • Advanced Member
  • 419 posts
  • LocationColombo, Sri Lanka | South Asia

Posted 02 July 2006 - 08:06 PM

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 :)

#10 bobleny

bobleny
  • Members
  • PipPipPip
  • Advanced Member
  • 429 posts

Posted 02 July 2006 - 08:16 PM

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 :)


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!
-- www.firemelt.net --
First do me a favor and read this: JavaScript is NOT Java - Then read this: www.php.net - When your done with that, read this Topic
After that, floors open. I and anyone else will be MORE than happy to answer YOUR query! [Topic Solved]
Cheer up, the worst has yet to come...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users