How to add leading zeroes in a php variable

Still couldn’t think of a catchy introduction. Maybe I should just stick to whatever comes to my mind first.
Anyway, in this another episode of my blog. I’m going to tell you how you can prefix a php variable preferably numbers with leading zeroes. Something like this:

00000018029

Well, don’t force your nerves too much. The number above doesn’t actually signify anything. I just used it to illustrate what leading zeroes are in case you came here by accident.

 

Introductory Example

Truth is, you don’t necessarily need to do this. Especially if you’re going to store the variable into a database later.
You know why? Well, just try making a query involving numbers with leading zeroes on it. And see what you get:

SELECT 00000000002

In my case, I get this cute little result:

image

I’m trying to be funny here. Just laugh if you feel like it.

Based on the example that I showed you above, mysql doesn’t actually care about the leading zeroes. And it will always return the number 1 no matter how many leading zeroes are out there.

 

Unsigned Zerofill

So what will we do then? Select the UNSIGNED ZEROFILL attribute when creating/updating a field:

image

Its just as simple as defining a field in phpmyadmin. But this time make sure that the datatype is actually a number. By number I mean any of the data types below:

  • int, tinyint, small int, medium int, big int
  • float
  • double

Also make sure to specify the length carefully.

This will automatically prefix your number with leading zeroes once it is saved to your database:

image

 


Presentation layer

So you don’t actually plan on saving all those zeroes in your database. Instead, you want the user to actually see those zeroes. Because you’re actually in the process of making a program that requires the number to be prefixed with zeroes. Like that in the generating of official receipt number or any transactions which requires numbers to have zeroes on it.

You can make use of the built-in php function called str_pad. As the name suggests, it’s a function which adds any prefix or suffix to any value you supply on it.

So if you want to prefix the word ‘dog’ with the letter ‘V’, you would want to do something like this:

str_pad('dog', 8 'V', STR_PAD_LEFT);

Here’s the format:

str_pad(A, B, C, D);

Where:

  • A – the original string that would be prefixed/suffixed with whatever you want
  • B – Maximum length of the string that would be generated.
  • C – What you actually want to add as a prefix/suffix
  • D – Where you want to append the string. There are 3 possible places (STR_PAD_LEFT, STR_PAD_RIGHT, STR_PAD_BOTH)

 


The jquery way

There’s a function that I found on this site. You can go ahead an visit the site. Its pretty straightforward and awesome. The function works like the str_pad in php. You can make use of the function to actually format numbers or any string by appending something to it.

 

Conclusion

Okay, so that’s it for this tutorial. We learned how to use the zerofill attribute for mysql fields. Use php to append something to a string. And lastly we learned how to format a string using jquery from the site which I pointed out above.

2 thoughts on “How to add leading zeroes in a php variable

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