Maarten’s Posterous

Internet Development 
« Back to blog

Leading zeros in PHP

This is how you add a certain number of leading zeros to integers in PHP.

$x = 1;
$y = sprintf("%03d",$x);
echo $y;

The 3 in "%03d" is the amount of digits you have in the end. So %02d if you only want 2 digits.

Source: Romeo, http://www.webmasterworld.com/forum88/4273.htm

Comments (0)

Leave a comment...

 
To leave a comment on this posterous, please login by clicking one of the following.
Posterous-login     Connect     twitter