<html>
<body>
<h2>Calculator</h2>
This calculator only supports 4 main functions: * / + - (multiply, divide, plus, and minus)
<form action="calculator.php" method="post">
<input type="text" size="15" name="num1" />
<select name="function">
<option value="*">*</option>
<option value ="/">/</option>
<option value ="+">+</option>
<option value ="-">-</option>
</select>
<input type="text" size="15" name="num2" />
<label>=</label>
<input type="submit" value="Calculate" />
</form>
<?php
$answer = "";
if (isset($_POST["num1"]) && isset($_POST["function"]) && isset($_POST["num2"])) {
$num1 = $_POST["num1"];
$num2 = $_POST["num2"];
$function = $_POST["function"];
if ($function == "*") {
$answer = $num1 * $num2;
}
else if ($function == "/" && $num2 !=0) {
$answer = $num1 / $num2;
}
else if ($function == "+") {
$answer = $num1 + $num2;
}
else if ($function == "-") {
$answer = $num1 - $num2;
}
else {
$answer = "Invalid input";
}
}
print "<br />";
print $answer;
?>
</body>
</html>