如何在php中为现有值添加值

Consider my variable $result to be 01212.... Now if i add 1 to my variable i get the answer 1213,but i want it to be 01213.... My php code is

echo sprintf('%02u', ($result+1));

Edit:

Answers to this question works.. But what happens if my variable is $result to be 0121212 in future...

You can use %05u instead on %02u

echo sprintf('%05u', ($result+1));

EDIT:

To generalize it:

<?php

$result = "0121211";

$len = strlen($result+1) + 1;

printf("%0${len}d", ($result+1)); // print 0121212


?>

you could try:

str_pad($result, 5, "0", STR_PAD_LEFT);

Read here: http://php.net/manual/en/function.sprintf.php

in sprintf, you also has to specify length you want - so in your case, if you want any number to be shown 5chars long, you haveto write

echo sprintf ('%05d', $d); // 5 places taking decimal

or even better

printf ('%05d', $d);

Maybe I'm missing something here but it could be as simple as

$result = '0'. ($result+1);

edit:

$test = array('01212', '0121212', '012121212121212', '-01212');
foreach( $test as $result ) {
  $result = '0'.($result+1);
  echo $result, "
";
}

prints

01213
0121213
012121212121213
0-1211

( you see, there are limitations ;-) )

Seems like you want to have a '0' in front of the number, so here would be the simplest :P

echo '0'. $result;

Or if you insist on using sprintf:

$newresult = $result + 1;
echo sprintf('%0'.(strlen(strval($newresult))+1).'u', $newresult);