[PHP] zero filled numbers with str_pad() function

To zero fill a number we can use PHP str_pad() function very easily

<?php
/** Example **/
$input = 1;
echo str_pad($input, 3);                     // produces "1"
echo str_pad($input, 3, "0", STR_PAD_LEFT); // produces "001"
echo str_pad($input, 3, "0", STR_PAD_BOTH);// produces "010"
echo str_pad($input, 3 , "0");            // produces "100"
?>

very easy right 😉

Advertisements

About Tanveer
RIA (Rich Internet Application) Developer with a wide variety of business applications. Particularly interested in client/server and relational database design. Always interested in migration projects, as well as close interaction with the DB manufacturers.

6 Responses to [PHP] zero filled numbers with str_pad() function

  1. Very easy indeed. Cheers for the explanation, was just what I needed!

  2. lhoanghieu says:

    thank for this.

  3. lucian says:

    tanx
    Reeeally handy!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: