Currency Converter in PHP

June 20, 2020
PHP
currency converter in php

In this tutorial, we will create a Basic Money Converter using PHP. This code will convert the given value to a specific currency value. The system uses a PHP POST to save the given value base on the currency options by doing the arithmetic formulas. This is a user-friendly kind of program feel free to modify it.

We will be using PHP as a scripting language and interpreter that is mainly used on any web server, including xamp, wamp, etc. It is being used to any popular websites, and it has a modern technology that can easily be used by the next generation.

Getting Started:

First, you have to download & install XAMPP or any local server that runs PHP scripts. Here’s the link for the XAMPP server https://www.apachefriends.org/index.html.

And, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple form for our application. To create a copy of the form and write it into your text editor, then save it as index.php.

<!DOCTYPE html>
<html lang="en">
    <head>
        <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
        <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
    </head>
<body>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodster</a>
        </div>
    </nav>
    <div class="col-md-3"></div>
    <div class="col-md-6 well">
        <h3 class="text-primary">PHP - Basic Money Converter</h3>
        <hr style="border-top:1px dotted #000;"/>
        <div class="col-md-3"></div>
        <div class="col-md-6">
            <form method="POST" action="">
                <div class="form-inline">
                    <select name="val" class="form-control">
                        <option value="PHP">PHP</option>
                        <option value="USD">USD</option>
                    </select>
                    <input class="form-control" type="number" name="digit"/>
                </div>	
                <br />
                <div class="form-inline">
                    <label>Select Currency: </label>
                    <select name="currency" required="required" class="form-control">
                        <option value="">Select an option</option>
                        <option value="USD">USD</option>
                        <option value="Euro">Euro</option>
                        <option value="PHP">PHP</option>
                        <option value="Japanese Yen">Japanese Yen</option>
                    </select>
                    <br /><br />
                    <center><button type="submit" name="convert" class="btn btn-primary form-control" style="width:30%;">Convert</button></center>
                    <br />
                </div>	
                    <?php require 'convert.php'?>
            </form>
        </div>
    </div>
</body>
</html>

Creating the Main Function

This code contains the main function of the application. This code will convert the money value when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as convert.php

<?php
    if(ISSET($_POST['convert'])){
        $val = $_POST['val'];
        $digit = $_POST['digit'];
        $currency = $_POST['currency'];
 
 
        if($val=="PHP" && $currency=="USD"){
            $output = $digit * 51.52;
            echo"<center><label class='text-success' style='font-size:25px;'>$".$output."</label></center>";
        }else if($val=="PHP" && $currency=="Euro"){
            $output = $digit * 63.62;
            echo"<center><label class='text-success' style='font-size:25px;'>€".$output."</label></center>";
        }else if($val=="PHP" && $currency=="PHP"){
            $output = $digit;
            echo"<center><label class='text-success' style='font-size:25px;'>₱".$output."</label></center>";
        }else if($val=="PHP" && $currency=="Japanese Yen"){
            $output = $digit * 0.47;
            echo"<center><label class='text-success' style='font-size:25px;'>¥".$output."</label></center>";
        }else if($val=="USD" && $currency=="USD"){
            $output = $digit;
            echo"<center><label class='text-success' style='font-size:25px;'>$".$output."</label></center>";
        }else if($val=="USD" && $currency=="Euro"){
            $output = $digit*0.89;
            echo"<center><label class='text-success' style='font-size:25px;'>€".$output."</label></center>";
        }else if($val=="USD" && $currency=="PHP"){
            $output = $digit/51.52;
            echo"<center><label class='text-success' style='font-size:25px;'>₱".$output."</label></center>";
        }else if($val=="USD" && $currency=="Japanese Yen"){
            $output = $digit*107.72;
            echo"<center><label class='text-success' style='font-size:25px;'>¥".$output."</label></center>";
        }
    }
?>

There you have it we successfully created Basic Money Converter using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

https://www.campcodes.com/

This is a free education portal. You can use every source code in your project without asking permission to the author. Share our website to everyone to make our community of programmers grow more.

    , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
    Comments
    • sir i need gold live price script for my blog plx upload this script on your website soon as soon possible thanks

      Adil Shahzad March 13, 2022 3:24 am Reply
    • How Do I add the Nigerian Naira in the script please? I tried changing the PHP value to NGN but it is not working.

      Don Richie October 7, 2021 4:18 am Reply
      • Did it throw an error? If it did you should include it here, it helps to know what you did wrong.

        FlilBare Hub November 1, 2021 6:38 pm Reply

    Leave a Reply

    Your email address will not be published. Required fields are marked *