PHP : displaying money with currency symbol in web page

While working on business applications you often have to deal with money and have to display it in your application. So in general what we are doing like storing value in database and fetch it and printing by echo with symbol attached as shown in below code.
$amount = 14236.00;
echo ” The Amount is $”.$amount;

But PHP also has some better facility than above code. PHP has money_format library function which can convert the number in to properly formatted with currency symbol.
Look at below code.
$number = 1234.56;
echo "<br/>".money_format("The price US dollar is %n", $number);

setlocale(LC_MONETARY, 'en_GB.UTF-8');
echo "<br/>". money_format("The price UK pound is %n", 1234.56); // £7.99

setlocale(LC_MONETARY, 'en_IN.UTF-8');
echo "<br/>".money_format('The price Indian rupee is %n', 211234.56); // ₹ 2,11,234.56
Output of the above code is as mentioned below.
The price US dollar is $1,234.56
The price UK pound is £1,234.56
The price Indian rupee is ₹ 2,11,234.56
You can see how money_format nicely set currency symbol and format the number with comma.
Important notice before money_format you have to call setlocale method and set ISO code for currency. If you are looking  for all available ISO codes click here.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your 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