title: Program Calculator Using PHP for computation and a HTML input form
body: <p>I'm a PHP newbie and I tried to setup a Calculator in PHP (HTML).</p>
  
  <p>Problem: </p>
  
  <p>I want to php script to call itself to process the input values.
  My goal is to avoid php session to cache the values. To be specific I have two different fields - one output field, which is readonly and a input field, where numeric values can be entered. In addition to that there are 5 different buttons to calculate addition, subtraction, multiplication, division and assign values in the input field.
  I was only able to solve the problem with Session Variables like in following example. I use a own-defined session, which I try to replace with normal PHP variables and I want to update them everytime one of the operation buttons are pressed. In addition the input values should be saved as well as the output to be able to use the same value again without the need of reentering it to the form.</p>
  
  <p>Basically, I want to replace all the session variables like "$_SESSION['output']" by php variables like "$output". All I've already tried was not working, because the values were overwritten each time I recall the script (or by calling itself).</p>
  
  <pre><code>&lt;?php session_start(); 
  
  // Session Timeout:
  if (isset($_SESSION['LAST_ACTIVITY']) &amp;&amp; (time() - $_SESSION['LAST_ACTIVITY'] &gt; 60)) {
      // last request was more than 1 minute ago
      session_unset();     // unset $_SESSION variable for the run-time 
      session_destroy();   // destroy session data in storage
  }
  $_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp
  ?&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
  &lt;title&gt;PHP Calculator&lt;/title&gt;
  &lt;style type="text/css"&gt;
  #add {
      color:black;
      background-color:#F55;
  }
  #subtract {
      color:black;
      background-color:#55f;
  }
  #multiply {
      color:black;
      background-color:#5f5;
  }
  #divide {
      color:black;
      background-color:#ff5;
  }
  #exp {
      color:black;
      background-color:#fa2;
  }
  #equal {
      color: black;
      background-color:#ccc;
  }
  .res {
      color: black;
      font-style: italic;
      background-color:#eee;
      border: none;
  }
  &lt;/style&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  
  &lt;?php 
      if (is_null($_SESSION['output'])){
          $_SESSION['output'] = "0.0"; 
      }
  
  // Operations to execute via form.
  $_SESSION['input'] = $_POST['input'];
  
  if(isset($_REQUEST['add'])){
      $_SESSION['output'] += $_POST['input']; 
  } else if (isset($_REQUEST['subtract'])) {
      $_SESSION['output'] -= $_POST['input']; 
  } else if (isset($_REQUEST['multiply'])) {
      $_SESSION['output'] *= $_POST['input']; 
  } else if (isset($_REQUEST['divide'])) {
      if($_POST['input'] == 0) {
          $_SESSION['output'] = $_POST['input'];  
      } else {
          $_SESSION['output'] /= $_POST['input']; 
      }
  } else if (isset($_REQUEST['exp'])) {
      $_SESSION['output'] = pow($_SESSION['output'], $_POST['input']);    
  } else if (isset($_REQUEST['equal'])) {
      $_SESSION['output'] = $_POST['input'];  
  } else {
      $_SESSION['input'] = 0.0;
  }
  ?&gt;
  
  &lt;form name="calculator" action="index.php" method="post"&gt;
  &lt;input class="res" type="text" readonly="readonly" size="23px" value="Result:" /&gt;
  &lt;input class="res" type="text" readonly="readonly" size="20px" value="Input:" /&gt;
  &lt;input class="res" type="text" readonly="readonly" size="10px" value="Operations:" /&gt;&lt;br&gt;
  &lt;input name="output" type="text" readonly="readonly"  value="&lt;?php print ($_SESSION['output'])?    &gt;"/&gt;
  &lt;input name="input" type="text" onfocus="this.value=''" value="&lt;?php print ($_SESSION['input'])?    &gt;" /&gt;
  &lt;input id="add" name="add" type="submit" value="+" /&gt;
  &lt;input id="subtract" name="subtract" type="submit" value="-" /&gt;
  &lt;input id="multiply" name="multiply" type="submit" value="*" /&gt;
  &lt;input id="divide" name="divide" type="submit" value="/" /&gt;
  &lt;input id="exp" name="exp" type="submit" value="^" /&gt;
  &lt;input id="equal" name="equal" type="submit" value="=" /&gt;
  &lt;/form&gt;
  &lt;/center&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>What is the most simple way to solve that?</p>