Creating Simple Currency Converter (Php) using PHP

In this tutorial we will create a Simple Currency Converter using PHP. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user-friendly environment. So Let's do the coding.

Getting started:

First you have to download & install XAMPP or any local server that run PHP scripts. In my case I used XAMPP as my local server, here's the link for XAMPP server https://www.apachefriends.org/index.html. I also used Bootstrap for the design https://getbootstrap.com/

The Main Function

This code contains the main function of the application, This code will try to convert the input digit then convert it based on the currency that use. To create this just write these block of code in the text editor, then name this file as convert.php

  1. <?php
  2.         if(ISSET($_GET['btn_submit'])){
  3.                 $digit = $_GET['txt_digit'];
  4.                 $currency = $_GET['currency'];
  5.                 if($digit != ""){
  6.                         switch($currency){
  7.                                 case "Dollar":
  8.                                         $output = $digit * 51.52;
  9.                                         echo"<center><label class='text-success' style='font-size:25px;'>$".(number_format($output))."</label></center>";
  10.                                 break;
  11.                                
  12.                                 case "Euro":
  13.                                         $output = $digit * 63.62;
  14.                                         echo"<center><label class='text-success' style='font-size:25px;'>&#8364;".(number_format($output))."</label></center>";
  15.                                 break;
  16.                                
  17.                                 case "Pound":
  18.                                         $output = $digit * 72.24;
  19.                                         echo"<center><label class='text-success' style='font-size:25px;'>&#163;".(number_format($output))."</label></center>";
  20.                                 break;
  21.                                
  22.                                 case "Chinese Yuan":
  23.                                         $output = $digit * 8.12;
  24.                                         echo"<center><label class='text-success' style='font-size:25px;'>&#165;".(number_format($output))."</label></center>";
  25.                                 break;
  26.                                
  27.                                 case "Japanese Yen":
  28.                                         $output = $digit * 0.47;
  29.                                         echo"<center><label class='text-success' style='font-size:25px;'>&#165;".(number_format($output))."</label></center>";
  30.                                 break;
  31.                         }
  32.                 }
  33.         }
  34. ?>

The Main Interface

This is the interface that display the output of the conversion,to make this just write these codes inside your text file, Then save it as index.php.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  5.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6.         </head>
  7. <body>
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodster</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">PHP - Simple Currency Converter</h3>
  16.                 <hr style="border-top:1px dotted #000;"/>
  17.                 <form method="GET" action="">
  18.                         <div class="form-inline">
  19.                                 <label>Php: </label>
  20.                                 <input class="form-control text-right" type="number" value="<?php echo isset($_GET['txt_digit']) ? $_GET['txt_digit'] : ''  ?>" name="txt_digit"/>
  21.                                 <label>Select Currency: </label>
  22.                                 <select name="currency" class="form-control">
  23.                                         <option value="">Select an option</option>
  24.                                         <option value="Dollar" <?php echo isset($_GET['currency']) && $_GET['currency'] == "Dollar" ? 'selected' : ''; ?>>Dollar</option>
  25.                                         <option value="Euro" <?php echo isset($_GET['currency']) && $_GET['currency'] == "Euro" ? 'selected' : ''; ?>>Euro</option>
  26.                                         <option value="Pound" <?php echo isset($_GET['currency']) && $_GET['currency'] == "Pound" ? 'selected' : ''; ?>>Pound</option>
  27.                                         <option value="Chinese Yuan" <?php echo isset($_GET['currency']) && $_GET['currency'] == "Chinese Yuan" ? 'selected' : ''; ?>>Chinese Yuan</option>
  28.                                         <option value="Japanese Yen" <?php echo isset($_GET['currency']) && $_GET['currency'] == "Japanese Yen" ? 'selected' : ''; ?>>Japanese Yen</option>
  29.                                 </select>
  30.                                 <br /><br />
  31.                                 <center><button type="submit" name="btn_submit" class="btn btn-primary form-control" style="width:30%;">Convert</button></center>
  32.                                 <br />
  33.                                 <?php require 'convert.php'?>
  34.                         </div>
  35.                 </form>
  36.         </div>
  37. </body>
  38. </html>

There you have it we successfully created a Simple Currency Converter using PHP. I hope that this simple tutorial helps you to what you are looking for. For more updates and tutorials just kindly visit this site.

Enjoy Coding :)

Add new comment