Simple Currency Converter in PHP using OOP Approach

By CampCodes Administrator

Updated on:

phpoop_currency

Getting Started

To improve the visual of this tutorial, I’ve used CDN for Bootstrap, so you need an internet connection for it to work.

Creating a Class

First, we are going to create our class, and we’re going to name this as Converter.php.

<?php
 
class Converter{
 
    private $rateValue;
 
    //I have base these rates on USD :)
    private $rates = [
        'USD' => 1.0,
        'GBP' => 0.7,
        'EUR' => 0.800284,
        'YEN' => 109.67,
        'CAN' => 1.23,
        'PHP' => 51.74,
    ];
 
    public function setConvert($amount, $currency_from){
        $this->rateValue = $amount/$this->rates[$currency_from];
    }
 
    public function getConvert($currency_to){
                return round($this->rates[$currency_to] * $this->rateValue, 2);
    }
 
    public function getRates(){
        return $this->rates;
    }
}
 
?>

In this class, we have defined our array of rates and different methods.

Creating our Convert Form

Next step is to create our form that converts our set amount from one currency to another.

<?php
 
session_start();
 
include_once('Converter.php');
 
$converter = new Converter();
 
$rates = $converter->getRates();
 
?>
<!DOCTYPE html>
<html>
<head>
    <title>Simple Currency Converter in PHP using OOP Approach</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
    <h1 class="page-header text-center">Simple Currency Converter in PHP using OOP Approach</h1>
    <div class="row">
        <div class="col-sm-4 col-sm-offset-4">
            <form method="POST" action="getconvert.php">
                <div class="form-group">
                    <label>Amount:</label>
                    <input type="text" class="form-control" name="amount">
                </div>
                <div class="form-group">
                    <label>From:</label>
                    <select class="form-control" name="currency_from">
                        <?php
                            foreach ($rates as $key => $currency){
                                ?>
                                <option value="<?php echo $key; ?>"><?php echo $key ?></option>
                                <?php
                            }
 
                        ?>
                    </select>
                </div>
                <div class="form-group">
                    <label>To:</label>
                    <select class="form-control" name="currency_to">
                        <?php
                            foreach ($rates as $key => $currency){
                                ?>
                                <option value="<?php echo $key; ?>"><?php echo $key ?></option>
                                <?php
                            }
 
                        ?>
                    </select>
                </div>
                <button type="submit" name="convert" class="btn btn-primary">Convert</button>
            </form>
 
            <?php
                if(isset($_SESSION['value'])){
                    ?>
                    <div class="alert alert-info text-center" style="margin-top:20px;">
                        <?php 
                            echo $_SESSION['value']['amount'].' '.$_SESSION['value']['from'].' is equal to '.$_SESSION['value']['result'].' '.$_SESSION['value']['to']; 
                        ?>
                    </div>
                    <?php
                    unset($_SESSION['value']);
                }
            ?>
        </div>
    </div>
</div>
</body>
</html>

In here, we fetch our rates, and we put them as options in our select tag.

READ ALSO:   How to Salt and Hash a Password using Sha256 in PHP

Creating our Convert Action

Lastly, we create our PHP code to convert the amount.

<?php
 
session_start();
 
require_once('Converter.php');
 
if(isset($_POST['convert'])){
    $amount = $_POST['amount'];
    $currency_from = $_POST['currency_from'];
    $currency_to = $_POST['currency_to'];
 
    $converter = new Converter();
    $converter->setConvert($amount, $currency_from);
    $result = $converter->getConvert($currency_to);
 
    $out = array();
    $out['amount'] = $amount;
    $out['from'] = $currency_from;
    $out['result'] = $result;
    $out['to'] = $currency_to;
 
    $_SESSION['value'] = $out;
    header('location:index.php');
 
}
else{
    header('location:index.php');
}
 
?>

That ends this tutorial. Happy coding!

Download Here

Leave a Comment