I have a variable that gets passed 3 digit numbers, when the variable is 19 I want it to be 019 so I can use it in a character string. How do I get it to keep the 0 in 019?
Hi,
You need to cast the variable as a string.
$varname = (string)$threedigitnumber;
Regards,
David
And, if you're variable is missing zeros before you turn it into a string, you could add the following:
$string=$yourvariable; for($i=1, strlen($string)<=3,$i++) $string = "0".$string;
$var = sprintf("%03d", $var);
mumble mumble Takes one fourth the time of my code too.
And remember boys and girls, for loops use ';' as section delimiters, not ','.
Hi All,
What about:
str_pad($input, 3, "0", STR_PAD_LEFT);
Cheers,
Thanks, thats why I like posting cause there are many different ways to do something.