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:
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.
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:
In my case, I get this cute little result:
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.
So what will we do then? Select the UNSIGNED ZEROFILL attribute when creating/updating a field:
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
Also make sure to specify the length carefully.
This will automatically prefix your number with leading zeroes once it is saved to your database:
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);
- 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.
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.