Skip to content

Instantly share code, notes, and snippets.

@tomlins95
Created October 20, 2014 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tomlins95/5e8af36e56ff587df804 to your computer and use it in GitHub Desktop.
Save tomlins95/5e8af36e56ff587df804 to your computer and use it in GitHub Desktop.
<?php
/* ======================================================
PHP Calculator example using "sticky" form (Version 1)
======================================================
Author : P Chatterjee (adopted from an original example written by C J Wallace)
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($calc)) { // $calc exists as a variable
$prod = $x * $y;
}
elseif(isset($Add)) {
$prod = $x + $y;
}
elseif(isset($Subtract)) {
$prod = $x - $y;
}
elseif(isset($Multiply)) {
$prod = $x * $y;
}
elseif(isset($Divide)) {
$prod = $x / $y;
}
else { // set defaults
$x=0;
$y=0;
$prod=0;
}
?>
<html>
<head>
<title>PHP Calculator Example</title>
</head>
<body>
<h3>PHP Calculator (Version 1)</h3>
<p>Multiply two numbers and output the result</p>
<form method="get" action="<?php print $_SERVER['PHP_SELF']; ?>">
x = <input type="text" name="x" size="5" value="<?php print $x; ?>"/>
y = <input type="text" name="y" size="5" value="<?php print $y; ?>"/>
<select>
<option value="Calculate">Calculate</option>
<option value="Clear">Clear</option>
<option value="Add">Add</option>
<option value="Subtract">Subtract</option>
<option value="Multiply">Multiply</option>
<option value="Divide">Divide</option>
</select>
</form>
<!-- print the result -->
<?php if(isset($calc)) { print "<p>x * y = $prod</p>"; }
else if(isset($Add)) { print "<p>x + y = $prod</p>";}
else if (isset($Subtract)) { print "<p>x - y = $prod</p>";}
else if(isset($Multiply)) { print "<p>x * y = $prod</p>";}
else if(isset($Divide)) { print "<p>x / y = $prod</p>";}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment