<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>PHP实现简单的计算器</title>
</head>
<body>
<form action="test.php" method="post">
<input type="text" name="num1" id="num1">
<select name="fuhao">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" name="num2" id="num2">
<input type="submit" name="tijiao" value="计算">
<?php
if(isset($_POST["tijiao"])){
$result=0;
switch ($_POST["fuhao"]){
case "+":
$result=$_POST["num1"]+$_POST["num2"];
break;
case "-":
$result=$_POST["num1"]-$_POST["num2"];
break;
case "*":
$result=$_POST["num1"]*$_POST["num2"];
break;
case "/":
$result=$_POST["num1"]/$_POST["num2"];
break;
}
}
?>
<?php
if($_POST["num1"]=="" || $_POST["num1"]==null){
echo "请输入第一个值";
return ;
}elseif($_POST["num2"]=="" || $_POST["num2"]==null){
echo "请输入第二个值";
return ;
}elseif(!is_numeric($_POST["num1"])){
echo "请输入合法数字";
return;
}elseif(!is_numeric($_POST["num2"])){
echo "请输入合法数字" ;
return;
}
echo "结果:{$_POST["num1"]} {$_POST["fuhao"]} {$_POST["num2"]} = {$result}";
?>
</form>
</body>
</html>
PHP简单的计算器代码
最新推荐文章于 2024-09-22 10:52:57 发布