View Tutorial Metadata Edit Content Revision History Add to Watchlist Add New Tutorial How to add leading zeros to a number in PHP

How to add leading zeros to a number in PHP

Adding of leading zeros in PHP is really easy using the sprintf function.

Example :

$x = 3;
print sprintf("%04d",$x);

which will print '0003'

Here are sprintf's docs : sprintf manual on php.net .

Only plain text supported.

Optional

Required - will be kept private

Optional

 
 

Rating: (0+, 0-) In: PHP 5