Skip to content

Instantly share code, notes, and snippets.

@matejkramny
Forked from tomlins95/gist:5e8af36e56ff587df804
Last active August 29, 2015 14:07
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 matejkramny/9c8d39be017e29d624e3 to your computer and use it in GitHub Desktop.
Save matejkramny/9c8d39be017e29d624e3 to your computer and use it in GitHub Desktop.
<?php
// NEVER DO THIS - BAD
//extract($_GET);
ini_set('display_errors', '1');
error_reporting(E_STRICT | E_ALL);
$prod = 0; $x = 0; $y = 0;
$action = "not set";
$formula = "";
if (isset($_GET['action']) && isset($_GET['x']) && isset($_GET['y'])) {
$action = $_GET['action'];
$x = floatval($_GET['x']);
$y = floatval($_GET['y']);
if (is_nan($x) || is_nan($y)) {
$x = 0;
$y = 0;
}
switch ($action) {
case 'add':
$prod = $x + $y;
$formula = "x + y";
break;
case 'subtract':
$prod = $x - $y;
$formula = "x - y";
break;
case 'multiply':
$prod = $x * $y;
$formula = "x * y";
break;
case 'divide':
$prod = $x / $y;
$formula = "x / y";
break;
}
}
?>
<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>
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 name="action">
<option value="add">Add</option>
<option value="subtract">Subtract</option>
<option value="multiply">Multiply</option>
<option value="divide">Divide</option>
</select>
<button type="submit">Calculate</button>
</form>
<!-- print the result -->
<p><?php print $formula; ?> = <?php print $prod; ?></p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment