Code Snippets → PHP → Force Leading Zero Force Leading Zero Chris Coyier on Feb 4, 2010 <?php function forceLeadingZero($int) { return (int)sprintf('%02d',$int); } ?> Forces leading zero to integers. was | now 1 | 01 2 | 02 3 | 03 10 | 10 100 | 100 99 | 99
str_pad?
str_pad($input, 2, “0”, STR_PAD_LEFT);
str_pad is probably more semantically accurate, but sprintf() is faster.
is correct.
return (int)… returns the same number.
return sprintf(‘%02d’,$int); is correct.
The solution isn’t correct. You shouldn’t return (int) if you want a leading ‘0’