PHP Currency converter tutorial
PHP Currency converter tutorial
This post will be a php currency converter tutorial where I’ll be showing you what I did in a few minutes based on the XML received directly from ECB (European Central Bank). The idea itself is simple and can be used in any projects you own / have. It can of course be developed further but this is just the basics for now. Ok, so lets get started, right?
PHP XML parsing
First we need to make sure we have our own XML file, it can be a link or a local file, but you need to make sure is public / have access to reading it.
For parsing our little XML file, we will use SimpleXML extension from PHP. It is really simple, see below:
1 2 3 4 5 |
<?php $xml=simplexml_load_file("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"); $date = $xml->Cube->Cube['time']; echo "Current date: ".$date."<br/>"; ?> |
In the above example we will read the “time” attribute for printing the current date of the currency. The output for this will be:
1 |
Current date: 2013-12-17 |
So basically that’s how you can get a value from the XML file.
Build get_rate() currency function
This function is really important, our XML file has its base currency in Euro, so we need to read our FROM and TO currency for conversion. You’ll understand this further in the second function. For now, below is a code snippet of the get_rate() function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php function get_rate($currency) { global $array_currency; for ($i=0;$i<count($array_currency);$i++) { if($array_currency[$i]['currency'] == "$currency") { $rate = $array_currency[$i]['rate']; break; } else { $rate = "99999999"; // setting an invalid rate just for testing / checking further in script } } return $rate; } ?> |
As you can see, we first use global $array_currency for us to pass to our function the $array_currency objects. Afterwards we count the objects and parse the array object. In case the currency does not exist, unfortunately the XML file I’m using does not have all currencies used with all countries, we will set a custom $rate of 99999999. This is just for using it further in our next function for testing / checking.
Build currency converter function
This here is the actual currency converter. It makes uses of the first function get_rate() for when it needs to change the base currency, or rather, the default FROM currency.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php function convert($from, $to, $amount) { global $array_currency; if($from === "EUR"){ $to = get_rate($to); if ($to != "99999999") { $result = round((double)$to * (double)$amount,4,PHP_ROUND_HALF_EVEN); } else { $result = "Can't convert one of the currencies used!"; } } else { $from_temp = get_rate($from); $to_temp = get_rate($to); if ($from_temp != "99999999" && $to_temp != "99999999" ) { $to = (double)$to_temp / (double)$from_temp; $result = round((double)$to * (double)$amount,4,PHP_ROUND_HALF_EVEN); } else { $result = "Can't convert one of the currencies used!"; } } return $result; } ?> |
Basically since our XML file already has a rate with FROM base currency Euro, we check if the base currency is EUR, else we proceed further and get the two rates FROM and TO currencies.
The precision of this currency converter is made with 4 decimals, while using PHP_ROUND_HALF_EVEN.
Running the currency conversion
Lastly you just need to call the currency convert function and you are done:
1 2 3 4 |
<?php $convert = convert("AUD","SGD", 18); echo "Conversion result: " .$convert; ?> |
Output will look similar to:
1 |
Conversion result: 20.1804 |
For those looking for the end code, they can use below snippet:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<?php $xml=simplexml_load_file("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"); $date = $xml->Cube->Cube['time']; echo "Current date: ".$date."<br/>"; $array_currency = $xml->Cube->Cube->Cube; function convert($from, $to, $amount) { global $array_currency; if($from === "EUR"){ $to = get_rate($to); if ($to != "99999999") { $result = round((double)$to * (double)$amount,4,PHP_ROUND_HALF_EVEN); } else { $result = "Can't convert one of the currencies used!"; } } else { $from_temp = get_rate($from); $to_temp = get_rate($to); if ($from_temp != "99999999" && $to_temp != "99999999" ) { $to = (double)$to_temp / (double)$from_temp; $result = round((double)$to * (double)$amount,4,PHP_ROUND_HALF_EVEN); } else { $result = "Can't convert one of the currencies used!"; } } return $result; } function get_rate($currency) { global $array_currency; for ($i=0;$i<count($array_currency);$i++) { if($array_currency[$i]['currency'] == "$currency") { $rate = $array_currency[$i]['rate']; break; } else { $rate = "99999999"; // setting an invalid rate just for testing / checking further in script } } return $rate; } $convert = convert("AUD","SGD", 18); echo "Conversion result: " .$convert; ?> |
That’s it for now, happy coding!