I have a number 1 through 9 but I need to make it a 2 digit number so it reads 01 or 05 or 07 . What is the code to do this?
There has to be a PHP function that does this... There is no way that everyone uses $num=0.$num
Let me rephrase what I want... I could have a number anywhere from 1-20
I want the number to be two digits no matter what
you can use the lpad function:
$num = lpad($num, 2, 0);
this will pad the left side with a zero if the length is less than 2.
thank you AARONBDAVIS....but lpad does not seem to work
result of $num is 03
I can not believe that there is not a PHP fuction that makes an interger to be two numbers big
I just wrote one. You can use that.
should be a conditional to check whether the number is less than 10 (single digit) i expect you won't want to pad out other numbers.
$twoDigitNumber = sprintf("%02d", $number);
// or to just output it as such:
printf("<p>The number is: %02d</p>\n", $number);
yep it's a matter of formatting output, so you should do that instead of actually modifying the number
firman wrote:I can not believe that there is not a PHP fuction that makes an interger to be two numbers big
Well the fact is integers do not have leading zeros. If you want leading zeros you must first convert to a string. The code below demonstrates the difference.
echo 02; // 2
echo '02'; // 02