two digits?

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
cto1mac
Forum Commoner
Posts: 54
Joined: Tue Jan 27, 2004 6:11 am
Location: Virginia Beach, VA

two digits?

Post by cto1mac »

Ok, here is what I am trying:

for($i = 1; $i <= 100; $i++) {
$var .= dechex($i);
}
print $var;

what it outputs is correct but I want is
00 instead of 0
01 instead of 1
....
and so forth how can I force the extra digit?

Thanks
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Code: Select all

str_pad($var,2,"0",STR_PAD_LEFT);
PHP Manual: [php_man]str_pad[/php_man]
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

printf("%02d",$value);
or

Code: Select all

sprintf("%02d",$value);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

hehe.. sometimes there are far too many ways to do the same thing.. ;)
Post Reply