Web Programming TutorialsLearn How to Create a Currency Converter Using PHP

Learn How to Create a Currency Converter Using PHP

A Currency Converter is a calculator that converts the value or quantity of one currency into the relative values or quantities of other currencies. For example, if you lived in the US and were traveling to another country, you would need to know the per dollar exchange rate for the currency of the country you were visiting. You would need to know the dollar- euro conversion, if you were traveling to certain parts of Europe, the dollar-pound conversion if you were in traveling in the United Kingdom, the dollar-yen conversion if you were traveling in Japan, the Canadian dollar – U.S. dollar conversion if you were traveling in Canada or the Swiss francs-dollar conversion if you were traveling in Switzerland.

A currency converter stores the most recent market valuations of the world’s currencies, which allows individuals to compare the value of one currency against those of others in the database.

In this article, we are going to build a simple currency calculator which is based on PHP as well as we are going to use the if condition and the basic html syntax. For this we need XAMPP or WAMPP.

Steps for installing Xampp: –
1. You have to visit the following link https://www.apachefriends.org/index.html Once you open the link, you have to download the installation file according to your operating system. Once the installation is finished, you have to go your installation drive ( I used the C drive) and click on Xampp folder. After clicking on XAMPP, you will be able to see multiple folders, click on htdocs.

In this folder, you can add your code. I attached the screen-shot, have a look:
htdocs
2. Now, we have to create a folder inside the htdocs named as converter. After that we have to create a file named currencyconvertor.php inside the converter folder. You can see the attached screen-shot:
converter

Steps for Implementation:
1. Now its time to start our coding part. Firstly we are going to design our form using basic html element after that we want to add the data in table format so I added table tag inside form. You will use the code stated below:

<html>
<head>

</head>

<body>

<form align="center" action="currencyconvertor.php" method="post">

<div id="box">
<h2><center>Currency Converter</center></h2>
<table>
	<tr>
	<td>
		Enter Amount:<input type="text" name="amount"><br>
	</td>
</tr>
<tr>
<td>
	<br><center>From:<select name='cur1'>
	 <option value="AUD">Australian Dollor(AUD)</option>
	 <option value="USD" selected>US Dollar(USD)</option>
	 </select>
</td>
</tr>
<tr>
	<td>
	<br><center>To:<select name='cur2'>
	 <option value="INR" selected >Indian Rupee(INR)</option>
	 <option value="JPY">Japanese Yen(JPY)</option>
	 <option value="PHP">PHilippine Peso(PHP)</option>
	
	</select>
</td>
</tr>
<tr>
<td><center><br>
<input type='submit' name='submit' value="CovertNow"></center>
</td>
</tr>
</table>
</form>

</body>
</html>

This will be your output:
currency-converter

2. Now after designing the form, we have to write a logic for currency converter. We used the IF condition as well as we used isset (). It is used to check whether a variable is set or not. If a variable is already unset with unset() function, it will no longer be set. The isset() function returns false if testing variable contains a NULL value.

After that, we are going to use $_POST(). The PHP provides $_POST associative array to access all the sent information using POST method. I attached the code below:

<?php
if(isset($_POST['submit'])){
	
$amount = $_POST['amount'];
$cur1 = $_POST['cur1'];
$cur2 = $_POST['cur2'];

if($cur1=="AUD" AND $cur2=="JPY"){
echo "<center><b>Your Converted Amount is:</b><br></center>";
echo "<center>" . $amount*82.463 . "</center>";
}

if($cur1=="AUD" AND $cur2=="INR"){
echo "<center><b>Your Converted Amount is:</b><br></center>";
echo "<center>" . $amount* 51.09 . "</center>";
}

if($cur1=="AUD" AND $cur2=="PHP"){
echo "<center><b>Your Converted Amount is:</b><br></center>";
echo "<center>" . $amount* 37.15 . "</center>";
}

if($cur1=="USD" AND $cur2=="JPY"){
echo "<center><b>Your Converted Amount is:</b><br></center>";
echo "<center>" . $amount* 109.49 . "</center>";
}

if($cur1=="USD" AND $cur2=="INR"){
echo "<center><b>Your Converted Amount is:</b><br></center>";
echo "<center>" . $amount* 67.83 . "</center>";
}

if($cur1=="USD" AND $cur2=="PHP"){
echo "<center><b>Your Converted Amount is:</b><br></center>";
echo "<center>" . $amount*49.32  . "</center>";
}
}

3. Now its time to add some CSS into your project so we are going to add the CSS part in header part using the style tag.

<style>
	#box
	{
		width:350px;
		height:270px;
		margin:0px auto;
		border:2px solid black;
	}
	h2{
		text-align: center;
	}
	table{
		margin:0px auto;
	}
</style>

After adding html, css and php, your code look like this:

<html>
<head>
<style>
	#box
	{
		width:350px;
		height:270px;
		margin:0px auto;
		border:2px solid black;
	}
	h2{
		text-align: center;
	}
	table{
		margin:0px auto;
	}
</style>
</head>

<body>

<form align="center" action="currencyconvertor.php" method="post">

<div id="box">
<h2><center>Currency Converter</center></h2>
<table>
	<tr>
	<td>
		Enter Amount:<input type="text" name="amount"><br>
	</td>
</tr>
<tr>
<td>
	<br><center>From:<select name='cur1'>
	 <option value="AUD">Australian Dollar(AUD)</option>
	 <option value="USD" selected>US Dollar(USD)</option>
	 </select>
</td>
</tr>
<tr>
	<td>
	<br><center>To:<select name='cur2'>
	 <option value="INR" selected >Indian Rupee(INR)</option>
	 <option value="JPY">Japanese Yen(JPY)</option>
	 <option value="PHP">Philippine Peso(PHP)</option>
	
	</select>
</td>
</tr>
<tr>
<td><center><br>
<input type='submit' name='submit' value="CovertNow"></center>
</td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit'])){
	
$amount = $_POST['amount'];
$cur1 = $_POST['cur1'];
$cur2 = $_POST['cur2'];

if($cur1=="AUD" AND $cur2=="JPY"){
echo "<center><b>Your Converted Amount is:</b><br></center>";
echo "<center>" . $amount*82.463 . "</center>";
}

if($cur1=="AUD" AND $cur2=="INR"){
echo "<center><b>Your Converted Amount is:</b><br></center>";
echo "<center>" . $amount* 51.09 . "</center>";
}

if($cur1=="AUD" AND $cur2=="PHP"){
echo "<center><b>Your Converted Amount is:</b><br></center>";
echo "<center>" . $amount* 37.15 . "</center>";
}

if($cur1=="USD" AND $cur2=="JPY"){
echo "<center><b>Your Converted Amount is:</b><br></center>";
echo "<center>" . $amount* 109.49 . "</center>";
}

if($cur1=="USD" AND $cur2=="INR"){
echo "<center><b>Your Converted Amount is:</b><br></center>";
echo "<center>" . $amount* 67.83 . "</center>";
}

if($cur1=="USD" AND $cur2=="PHP"){
echo "<center><b>Your Converted Amount is:</b><br></center>";
echo "<center>" . $amount*49.32  . "</center>";
}


}

?>

</body>
</html>

currency-converter-page
convert-currency
I hope this has been helpful for you guys in understanding a how to create a currency convertor using php.. Goodluck!

2 COMMENTS

  1. Philippine Money Changer

    Amount:

    Currency:

    Korean Won
    US Dollar
    Singapore Dollar

    <?php
    $currency=$_POST['currency'];
    $amount=$_POST['amount'];

    if ($currency=='won')
    {
    echo "Your Converted Amount is:“;
    echo $amount*21.51941;
    }
    else if($currency==’usd’)
    {
    echo “Your Converted Amount is:“;
    echo amount*50.6112;
    }
    else ($currency==’sd’)
    {
    echo “Your Converted Amount is:“;
    echo amount*37.4800;
    }

    ?>

    Hi! I am novice on using PHP. This is so far my second output but doesn’t work at all.

    When I check the localhost, it says
    Parse error: syntax error, unexpected ‘{‘ in C:\xampp\htdocs\Activity 2\Converter.php on line 18.

    Why is that? Need your help.
    Thank you.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -