Php Code:
<?php
echo "<p>simple php calculator</p>";
echo "<form action='' autocomplete='off' method='GET'>";
$mydefault="";
if (isset($_GET["a"])) {
echo "<input type='text' name='a' value='" . $_GET["a"] . "'>";
$mydefault=$_GET["operator"];
}
else
echo "<input type='text' name='a'>";
echo "<select name='operator'>
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
<option value=" . $mydefault . " selected>" . $mydefault . "</option></select>";
if (isset($_GET["b"]))
echo "<input type='text' name='b' value='" . $_GET["b"] . "'>";
else
echo "<input type='text' name='b'>";
echo "=";
if (isset($_GET["a"])) {
$a=(int)$_GET["a"];
$b=(int)$_GET["b"];
$operator=$_GET["operator"];
switch ($operator) {
case "+":
echo $res=$a+$b;
break;
case "-":
echo $res=$a-$b;
break;
case "*":
echo $res=$a*$b;
break;
case "/":
if ($b > 0)
echo $res=$a/$b;
break;
}
}
echo "<br><br>";
echo "<input type='submit' value='Calculate'>";
echo "</form>";
?>