Currency converter in PHP

Create currency converter in PHP


In this article, we will be creating a simple currency converter in PHP. You just need to tell the source and target currency and the amount. You can check the demo as well.


We will be using a FREE api to convert any currency to anyother at the standard market rates. Market rates may differ on time, also it may take some hours for the API to update the rates if the currency goes up or down.

You just have to provide both currencies and the amount you want to convert, and it will be converted accordingly.

No external library has been used in this script.

We start off by creating a simple form to get the source and destination currency, and the amount he wants to convert. For example, if you want to convert 143 USD dollars to PKR:

  • Source currency = USD
  • Destination currency = PKR
  • Amount = 143

Simple !

We will be creating just 2 files:

  1. index.php to get user input.
  2. convert.php to do the conversion.
<form method="POST" action="convert.php">
    <input type="text" name="from" placeholder="From" /> <br />
    <input type="text" name="to" placeholder="To" /> <br />
    <input type="number" name="amount" placeholder="Amount" /> <br />
    <input type="submit" value="Convert" /> <br />


In this file, first we have to get the user input and save it in separate variable:

    $from = $_POST["from"];
    $to = $_POST["to"];
    $amount = $_POST["amount"];

Then, we have to call the PHP built-in curl function and pass these variables as parameters to do the conversion. The URL of API is:{your_api_key}

Get API key

You just have to change the currency according to yours (from the variables) and it will return the standard rate of 1 unit. For example, right now, the above URL will return 135. It is the value of 1 USD in PKR currency.

$string = $from . "_" . $to;
$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => "" . $string . "&compact=ultra&apiKey={your_api_key}",
  • CURLOPT_RETURNTRANSFER this will return the output as string in curl_exec() function instead of outputting it directly.
  • $string variable is created separately because it will be re-used in the next step.

Execute the curl command:

$response = curl_exec($curl);

At this point, when you fill the form and submit you will see something similar to the output:


This is the response in JSON format. You have to convert it to PHP objects and array format:

$response = curl_exec($curl);
$response = json_decode($response, true);
  • 2nd parameter to json_encode() function is true, which make sure the string will be converted in PHP array.

Now, it will print something like this:

Array ( [USD_PKR] => 141.590271 )

This is the conversion rate of currency of 1 unit. You can get it’s value in a variable:

$rate = $response[$string];

And can find the total by multipying this with the number of dollars user entered:

$total = $rate * $amount;

Now, you will be able to view the actual price after conversion. So your currency converter in PHP is ready. If you have any problem in following this, kindly do let us know.

How useful was this post

Click on a star to rate it!

Average rating 4 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post

Leave a Reply

Please disable your adblocker or whitelist this site!