inWebdeveloper

Free and Open Source Web Resources

Recent Posts

If you want to format a number with leading or trailing zeroes, you can use this method :

1. Use the printf( ) or sprintf( ) function

//Source : http://stackoverflow.com/a/6457950

$number = 9;
$paddedNum = sprintf("%04d", $number);
echo $paddedNum; // prints 0009

2. Use the str_pad() function

//Source : http://stackoverflow.com/a/6457981

//$number = 9;
echo str_pad($number, 4, '0', STR_PAD_LEFT); // output: 0009

OR


//Source : http://www.php.net/manual/en/function.str-pad.php#86404
function number_pad($number,$n) {
	return str_pad((int) $number,$n,"0",STR_PAD_LEFT);
}

echo number_pad($number,4);

Related Reading:

Learning PHP, MySQL, and JavaScript: A Step-By-Step Guide to Creating Dynamic Websites (Animal Guide)
PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (4th Edition)
PHP Cookbook: Solutions and Examples for PHP Programmers
PHP Solutions: Dynamic Web Design Made Easy
I Hate PHP-A Beginners Guide to PHP & MySQL Programming Without Brain Overload!

Incoming search terms:

Categories: PHP

Leave a Reply


Popular stories

Free Download: Angry Birds...

Posted on Jun - 24 - 2011

47 Comments

phpSysInfo : PHP script...

Posted on Mar - 22 - 2010

10 Comments

Free SQL Editor :...

Posted on Jul - 24 - 2010

10 Comments

Free Download Samsung Kies...

Posted on May - 7 - 2011

9 Comments

MobiOne : Free iPhone...

Posted on Apr - 18 - 2010

7 Comments

Click here for 5 Day Trial Promotion