Simple Currency Converter in PHP using OOP Approach

Getting Started

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

Creating our Class

First, we are going to create our class and we're going to name this as Converter.php.
  1. <?php
  2.  
  3. class Converter{
  4.  
  5. private $rateValue;
  6.  
  7. //I have base these rates on USD :)
  8. private $rates = [
  9. 'USD' => 1.0,
  10. 'GBP' => 0.7,
  11. 'EUR' => 0.800284,
  12. 'YEN' => 109.67,
  13. 'CAN' => 1.23,
  14. 'PHP' => 51.74,
  15. ];
  16.  
  17. public function setConvert($amount, $currency_from){
  18. $this->rateValue = $amount/$this->rates[$currency_from];
  19. }
  20.  
  21. public function getConvert($currency_to){
  22. return round($this->rates[$currency_to] * $this->rateValue, 2);
  23. }
  24.  
  25. public function getRates(){
  26. return $this->rates;
  27. }
  28. }
  29.  
  30. ?>
In this class we have define our array of rates and the different methods.

Creating our Convert Form

Next step is to create our form that converts our set amount from one currency to another.
  1. <?php
  2.  
  3.  
  4. include_once('Converter.php');
  5.  
  6. $converter = new Converter();
  7.  
  8. $rates = $converter->getRates();
  9.  
  10. ?>
  11. <!DOCTYPE html>
  12. <html>
  13. <head>
  14. <title>Simple Currency Converter in PHP using OOP Approach</title>
  15. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  16. </head>
  17. <body>
  18. <div class="container">
  19. <h1 class="page-header text-center">Simple Currency Converter in PHP using OOP Approach</h1>
  20. <div class="row">
  21. <div class="col-sm-4 col-sm-offset-4">
  22. <form method="POST" action="getconvert.php">
  23. <div class="form-group">
  24. <label>Amount:</label>
  25. <input type="text" class="form-control" name="amount">
  26. </div>
  27. <div class="form-group">
  28. <label>From:</label>
  29. <select class="form-control" name="currency_from">
  30. <?php
  31. foreach ($rates as $key => $currency){
  32. ?>
  33. <option value="<?php echo $key; ?>"><?php echo $key ?></option>
  34. <?php
  35. }
  36.  
  37. ?>
  38. </select>
  39. </div>
  40. <div class="form-group">
  41. <label>To:</label>
  42. <select class="form-control" name="currency_to">
  43. <?php
  44. foreach ($rates as $key => $currency){
  45. ?>
  46. <option value="<?php echo $key; ?>"><?php echo $key ?></option>
  47. <?php
  48. }
  49.  
  50. ?>
  51. </select>
  52. </div>
  53. <button type="submit" name="convert" class="btn btn-primary">Convert</button>
  54. </form>
  55.  
  56. <?php
  57. if(isset($_SESSION['value'])){
  58. ?>
  59. <div class="alert alert-info text-center" style="margin-top:20px;">
  60. <?php
  61. echo $_SESSION['value']['amount'].' '.$_SESSION['value']['from'].' is equal to '.$_SESSION['value']['result'].' '.$_SESSION['value']['to'];
  62. ?>
  63. </div>
  64. <?php
  65. unset($_SESSION['value']);
  66. }
  67. ?>
  68. </div>
  69. </div>
  70. </div>
  71. </body>
  72. </html>
In here, we fetch our rates and we put them as options in our select tag.

Creating our Convert Action

Lastly, we create our PHP code to convert the amount.
  1. <?php
  2.  
  3.  
  4. require_once('Converter.php');
  5.  
  6. if(isset($_POST['convert'])){
  7. $amount = $_POST['amount'];
  8. $currency_from = $_POST['currency_from'];
  9. $currency_to = $_POST['currency_to'];
  10.  
  11. $converter = new Converter();
  12. $converter->setConvert($amount, $currency_from);
  13. $result = $converter->getConvert($currency_to);
  14.  
  15. $out = array();
  16. $out['amount'] = $amount;
  17. $out['from'] = $currency_from;
  18. $out['result'] = $result;
  19. $out['to'] = $currency_to;
  20.  
  21. $_SESSION['value'] = $out;
  22. header('location:index.php');
  23.  
  24. }
  25. else{
  26. header('location:index.php');
  27. }
  28.  
  29. ?>
That ends this tutorial. Happy coding :)

Add new comment