I am retreiving a number from an ID field in the database, the number is stored in the database as 0036. I want to display this number but incremented by 1 in my PHP script. I tried doing this but instead of displaying 0037 it just displays 37. How can I make this work? This is what I tried doing: $value=$row['id']+1; Code (markup):
you would have to do something like $tmp = intval($row['id'] + 1); $len = strlen($tmp); if($len == 1) $value = '000'.$tmp; elseif($len == 2) $value = '00'.$tmp; elseif($len == 3) $value = '0'.$tmp; else $value = $tmp; PHP:
Or, if you don't enjoy doing so much typing, you can just use: $value = sprintf("%04d", $row['id'] + 1); Code (markup):
Cheers SmallPotatoes. Will that function also work if the number is for example: 0123 And I want it to increment by one to make it 0124
Yep, it will work with any number from 1-4 digits. If the number is more than 4 digits it will just leave it alone. e.g., 255 becomes 0255, but 12345 remains 12345. To change the number of digits it pads out to, just change the 4 in the first argument to sprintf.