Reputation: 12957
I've a variable titled $value
as follows :
$value = 5985;
If I echo 05985;
it prints 15. I understood that PHP considers this number as octal and prints 15 but I don't want 15 I want leading zero/zeroes prefixed to a value contained in a variable $value
.
But I want to make it five or say six digits long. In that case I need to add leading zero/zeroes to the value contained in a variable $value
. When I add those leading zeroes and echo the variable $value
it should look like as follows :
05985; //If I want five digit number
005985; //If I want six digit number
I searched and tried following approach but it didn't work out.
str_pad($value, 8, '0', STR_PAD_LEFT);
sprintf('%08d', $value);
echo $value;
So can someone please suggest me how to do that?
Thanks.
Upvotes: 1
Views: 1879
Reputation: 3874
Sprintf does not directly change the value of its second parameter, it returns the result. Try
$value = sprintf('%08d', $value);
Upvotes: 2
Reputation: 11213
You need to treat it as a string.
$number = '05433';
Echo $number;
Should give you what you want. You can also add a zero like this
$number = '0' . $number;
Upvotes: 0