Skip to content

Instantly share code, notes, and snippets.

@pchatterjee
Created September 19, 2019 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pchatterjee/00aed5faabbd363979b771e58bfe5280 to your computer and use it in GitHub Desktop.
Save pchatterjee/00aed5faabbd363979b771e58bfe5280 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;
}
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; ?>"/>
<input type="submit" name="calc" value="Calculate"/>
<input type="reset" name="clear" value="Clear"/>
</form>
<!-- print the result -->
<?php if(isset($calc)) {
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