<?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: 15 Oct 2007

*/

// grab the form values from $_HTTP_POST_VARS hash
extract($_GET);

// first compute the output, but only if data has been input
   if(isset($multiply)) { // $calc exists as a variable
      $prod = $x * $y;
   } elseif(isset($add)) {
     $prod = $x + $y;
   } elseif(isset($subtract)) {
     $prod = $x - $y;
   } elseif(isset($divide)) {
     $prod = $x / $y;
   } else { // set defaults
      $x=0;
      $y=0;
      $prod=0;
   }
/*
   if (isset($clear)) {
     $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 2)</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="+">+</option>
         </select>
         y =  <input type="text" name="y" size="5" value="<?php  print $y; ?>"/>


         <input class="button" type="submit" name="multiply" value="Multiply"/>
         <input class="button" type="submit" name="add" value="Add"/>
         <input class="button" type="submit" name="subtract" value="Subtract"/>
         <input class="button" type="submit" name="divide" value="Divide"/>
         <input class="button" type="submit" name="clear" value="Clear"/>
      </form>

      <!-- print the result -->
      <p class="result"><?php
      if(isset($multiply)) {

         print "$x * $y = $prod";

      } elseif (isset($add)) {

        print "$x + $y = $prod";

      }  elseif (isset($subtract)) {

        print "$x - $y = $prod";

      }  elseif (isset($divide)) {

        print "$x &divide; $y = $prod";

      } else {
        echo "Result will show here.";
      }
      ?></p>

      </div>
   </body>
</html>
