NYCPHP Meetup

[nycphp-talk] PHP addition and leading zeros

Ken Robinson kenrbnsn at rbnsn.com
Tue Jan 30 23:32:20 EST 2007


At 11:23 PM 1/30/2007, Aaron Fischer wrote:
>I am retrieving a four digit value from a mysql db.  It is an
>unsigned zerofill field so there are leading zeros.
>
>I add 0001 to the value.  However, I find that I lose my leading
>zeros, which I want to keep.  I also tried incrementing the value but
>that produces the same result.

Use the sprintf() function, <http://www.php.net/sprintf>, to put the 
leading zeros back into the string:

$num = 1;
$num_leading_zeros = sprintf("%04d",$num);
echo $num . ' ----- ' . $num_leading_zeros;

Ken 




More information about the talk mailing list