This may seem like an odd question, but it’s something I couldn’t solve on my own. If I were to say, have a number such as 001 assigned to a variable $add, then wanted to perform an operation like this:
$me = $add + 1;
How could I keep the “useless” leading 0s in this number after the operation?
Full overview:
<?php
$add = 001;
$me = $add + 1;
?>
My desired output is 002, but my received output is simply 2. I also wish to be able to do this backwards, say minus 1 from 002 yielding 001 instead of simply 1.
If you always want the number to have three digits, you can use sprintf
If the number of digits is variable and depends on the original input, you have to first determine how many digits you want, and then put that in the format parameter.
Also, when getting the starting value, make sure that you don’t accidentally get it in octal (which should not really happen when parsing an input string, those will be treated as decimal, but careful with literals in the program).