<?php
$result;
$num1 = isset($_GET['num1']) ? $_GET['num1'] : 0;
$num2 = isset($_GET['num2']) ? $_GET['num2'] : 0;
$cal = isset($_GET['cal'])? $_GET['cal'] : 0;
switch ($cal) {
case '+':
$result = $num1+$num2;
break;
case '-':
$result = $num1-$num2;
break;
case '*':
$result = $num1 * $num2;
break;
case '/':
if ($num2 != 0) {
$result = $num1 / $num2;
} else {
echo "被除数不可以为0";
}
break;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>计算器</title>
</head>
<body>
<h2>计算器</h2>
<form action="" method="get">
请输入第一个数<input type="text" name="num1"
value="<?php echo $num1 ?>"><br>
<input type="radio" name="cal" value="+" <?php if ($cal=="+") {
echo "checked";
}?>>+
<input type="radio" name="cal" value="-" <?php if ($cal=="-") {
echo "checked";
}?>>-
<input type="radio" name="cal" value="*" <?php if ($cal=="*") {
echo "checked";
}?>>*
<input type="radio" name="cal" value="/" <?php if ($cal=="/") {
echo "checked";
}?>>/<br>
请输入第二个数 <input type="text" name="num2"
value="<?php echo $sum2 ?>">
<br>
<button>提交</button>
</form>
<?php
echo "<h6>$num1 {$cal} $num2 = {$result}</h6>";
?>
</body>
</html>