I need a suggestion on a function for a php counter. Is there any function to have numbers with 5 digit as 00001, or 00123… this number should be not random but have to increase the value of a previous field.
If a number is $n=’00001’ there is a function to increase by one and get 00002 and not 2?
Thanks F.
$new = str_pad(ltrim($old, '0') + 1, 5, '0', STR_PAD_LEFT);
- note that converting a string directly to an integer if it has leading zeros may not produce the results you expect, it may be treated as an octal. Don't underestimate PHP's++
increment operator, it is pretty clever and can actually cope with alphas as well as numerics, like this example$n = sprintf('%1$05d', $number);