<?php
$sum="";
$num1=$_POST['num1'];
$num2=$_POST['num2'];
if(is_numeric($num1) && is_numeric($num2)){ //is_numeric 判断是不是为数字
if(isset($_POST['div'])){
if($num2 ===0){
echo "除数不能为0";
}else{
$sum = number_format($num1/$num2,2);
}
}
if(isset($_POST['add'])){
$sum = number_format($num1+$num2,2);
}
if(isset($_POST['sub'])){
$sum = number_format($num1-$num2,2);
}
if(isset($_POST['mul'])){
$sum=number_format($num1*$num2,2); //取小数点后两位 或者用round()、四舍五入;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form method="post" action="">计算器
数值1:<input type="text" name="num1" value="<?php echo $num1;?>">
数值2:<input type="text" name="num2" value="<?php echo $num2;?>">
<input type="submit" name="add" value="+">
<input type="submit" name="sub" value="-">
<input type="submit" name="mul" value="*">
<input type="submit" name="div" value="/">
结果:<input type="text" name="sum" value="<?php echo $sum;?>">
</form>
</body>
</html>