Page 1 of 1

strings as numbers

Posted: Wed Feb 05, 2003 4:10 pm
by uberpolak
I'm wondering if there's a way to force an incremented value to come out as two digits. I tried making it a string, like so:

Code: Select all

<?php

for ($zz = "01"; $zz <= 31; $zz++)
{
 echo "<OPTION VALUE="" . $zz . "">" . $zz . "</OPTION>";
}

?>
but no luck, only the first value is two digits (and those greater than 9, obvioiusly). Anybody have any ideas as to how I could accomplish this?

Posted: Wed Feb 05, 2003 4:45 pm
by lazy_yogi
You could use :

Code: Select all

for ($zz = "01"; $zz <= 31; $zz++) 
{
      if (strlen ($zz) == 1) $zz = "0".$zz;
      echo $zz."<BR>" ;
}

Posted: Thu Feb 06, 2003 3:36 am
by volka
or printf
e.g.

Code: Select all

<pre><?php
for($i=1; $i<2000; $i*=2)
	printf("%02d\n", $i);
?></pre>