-
-
Save matejkramny/9c8d39be017e29d624e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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