Edited: 3rd August, 2018
Every web developer knows that most times you have the need to store numbers in your database. This could be price of goods, large numbers of inventory or just about anything that requires a numerical value. While you may feel comfortable to store thirty million as 30000000 in your database (and you should); you wouldn't just want to display it that way to the web users. You would want to display it as something like this 30,000,000. The catch is to know how to implement this comma-formatted number in your webpage and apps using PHP. That is the essence of this tutorial.
Actually, formatting your numbers with commas in PHP does not require elaborate programming, but if you do not know this, you could spend helpless hours trying to figure out how to do this. I remember the first time I needed to do this. I had no idea how and it took me lots of research on books and online before I could discover the PHP number_format() function.
The number_format() function enables you to comma format your numerical values. To keep it simple, we have to note that in its most basic form, number_format() accepts a number to format, and returns a string containing the formatted number, rounded to the nearest whole number, with commas between each group of thousands. Below is a simple PHP code that does this:
$var = 300000;
$formatted_var = number_format($var);
echo $formatted_var;
This should output: 300,000.
$var = number_format(30000, 2);
echo $var;
Simple, right. Go ahead and use this amazing PHP function to improve your display on your webpage. To learn more about the PHP number_format() function, refer to the official PHP page for this number: PHP number format
If you find this helpful, please do leave a comment and share this post to help someone else. I intend to put up tutorials that will help some little but disturbing programming problem. So you can also suggest what you want me to write on next! Thank you.