<?php

/* ======================================================
   PHP Calculator example using "sticky" form (Version 1)
   ======================================================

   Author : P Chatterjee (adopted from an original example written by C J Wallace)
   Extended by

   Purpose : To multiply 2 numbers passed from a HTML form and display the result.

   input:
      x, y : numbers
      calc : Calculate button pressed


   Date: 30 Sep 2016

*/

// grab the form values from $_HTTP_POST_VARS hash
extract($_GET);


function calculate($x, $y, $operation){
    switch ($operation) {
        case "+":
        return $x + $y;
        break;

        case "-":
        return $x - $y;
        break;

        case "*":
        return $x * $y;
        break;

        case "/":
        if ($y == 0)
          return "&infin;";
        else
          return $x / $y;
        break;

    } // switch $operation
} // # function calculate

if (!isset($calc))  {
  $x=0;
  $y=0;
  $prod=0;
}

?>

<html>
   <head>
      <title>PHP Calculator Example</title>
      <link href="https://fonts.googleapis.com/css?family=Karla|Roboto+Mono" rel="stylesheet">
      <link rel="stylesheet" href="style.css" media="screen" title="no title" charset="utf-8">
   </head>

   <body>
      <div class="container">


      <h3>PHP Calculator (Version 5)</h3>
      <p>This program can perform four basic arithmetic functions.</p>
      <p>It can add, subtract, divide and multiply values.</p>

      <form method="get" action="<?php print $_SERVER['PHP_SELF']; ?>">

         x = <input type="text" name="x" size="5" value="<?php   print $x; ?>"/>
         <select class="" name="operation">
           <option value="+" <?php if(isset($operation) && $operation == "+"){ echo 'selected="selected"';} ?>>+</option>
           <option value="-" <?php if(isset($operation) && $operation == "-"){ echo 'selected = "selected"';} ?>>-</option>
           <option value="*" <?php if(isset($operation) && $operation == "*"){ echo 'selected = "selected"';} ?>>x</option>
           <option value="/" <?php if(isset($operation) && $operation == "/"){ echo 'selected = "selected"';} ?>>&divide;</option>
         </select>
         y =  <input type="text" name="y" size="5" value="<?php  print $y; ?>"/>

         <input class="button" type="submit" name="calc" value="Calculate"/>
         <input class="button" type="submit" name="clear" value="Clear"/>
      </form>

      <!-- print the result -->
      <p class="result"><?php
      if(isset($calc)) {
         print "$x $operation $y = " . calculate($x, $y, $operation);
      }  else {
        echo "Result will show here.";
      }
      ?></p>

      <h4>Other versions.</h4>
      <a href="calculator_v4.php">Version 4</a>
      <br><br>
      <a href="calculator_v3.php">Version 3</a>
      <br><br>
      <a href="calculator_v2.php">Version 2</a>
      <br><br>
      <a href="calculator_v1.php">Version 1</a>
      <br><br>
      <pre>
        <?php
          if (isset($_GET['operation'])) {
            # echo "The operation value exists.";
          }
        ?>
      </pre>
      </div>
   </body>
</html>
