strings as numbers

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
uberpolak
Forum Contributor
Posts: 261
Joined: Thu Jan 02, 2003 10:37 am
Location: Next to the bar

strings as numbers

Post 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?
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post 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>" ;
}
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

or printf
e.g.

Code: Select all

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