I have this variable $value1= 000001;
which as you can see contains some numbers which begins with 0.
What I am trying to is add "1" with the variable so that when I echo it looks like this 000002
. Again for example if the variable is like this $value1= 012001;
I want to make it look like 012002
.
I have tried like following but the result it produces is only "2"
$value1= 000001;
echo $value+1;
Could you please show me how to achieve this?
thanks :)
Many ways to do this, here's a different one:
$value1 = '000101';
$strLen = strlen($value1);
$value = (((int) $value1) + 1);
echo str_pad($value, $strLen, '0', STR_PAD_LEFT);
php test.php 000102
Use printf
<?php
$value1 = 2;
printf("%06d", 2);
Correction: number_format wouldn't be useful here, removed that.
Addendum: unless you're intending to work with octal numbers, don't start a numeric literal with a 0 as PHP interprets numbers starting with 0 as being octal.
I'd go with str_pad
, as it's faster and, more importantly: more readable, than printf
(number_format
isn't relevant here). echo str_pad($value+1, 6, '0', STR_PAD_LEFT);
$value1= 000001;
printf( "%06d", $value + 1 );