php写一个计算器
16lz
2021-04-29
<?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>
更多相关文章
- Vue自学之路10-简单的计算器
- C语言实现计算器
- 解锁苹果Mac计算器的隐藏功能,不止加减乘除
- 神器! 统计和金融计算器, 词云和情感分析器强大到无敌!
- android开发简单的计算器
- android应用程序--计算器
- Android(安卓)基于源码的科学计算器——Calculator浅析
- [房贷计算器]-升级心得
- Android中的TextView 、EditText、Activity、Intent实例小计算器