用php简单实现加减乘除计算器
(编辑:jimmy 日期: 2024/11/19 浏览:3 次 )
用php实现加减乘除计算器。代码很简单哦!
复制代码 代码如下:
<?php
header("content-type:text/html;charset=utf-8");
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>简单计算机</title>
</head>
<body>
<form action="jisuan.php" method="post">
第一个数<input type="text" value="" name="num1"><br />
计算符号<select name="oper">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select><br />
第二个数<input type="text" value="" name="num2"><br />
<input type="submit" value="计算结果"><br />
</form>
</body>
</html>
<?php
$num1=$_POST['num1'];
$num2=$_POST['num2'];
$oper=$_POST['oper'];
$rs=0;
switch($oper){
case "+":
$rs=$num1+$num2;
break;
case "-":
$rs=$num1-$num2;
break;
case "*":
$rs=$num1*$num2;
break;
case "/":
$rs=$num1/$num2;
break;
default: echo "您输入的不正确";
}
$_SESSION['rs']=$rs;
echo '计算结果为:'.$_SESSION['rs'];
?>
复制代码 代码如下:
<?php
header("content-type:text/html;charset=utf-8");
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>简单计算机</title>
</head>
<body>
<form action="jisuan.php" method="post">
第一个数<input type="text" value="" name="num1"><br />
计算符号<select name="oper">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select><br />
第二个数<input type="text" value="" name="num2"><br />
<input type="submit" value="计算结果"><br />
</form>
</body>
</html>
<?php
$num1=$_POST['num1'];
$num2=$_POST['num2'];
$oper=$_POST['oper'];
$rs=0;
switch($oper){
case "+":
$rs=$num1+$num2;
break;
case "-":
$rs=$num1-$num2;
break;
case "*":
$rs=$num1*$num2;
break;
case "/":
$rs=$num1/$num2;
break;
default: echo "您输入的不正确";
}
$_SESSION['rs']=$rs;
echo '计算结果为:'.$_SESSION['rs'];
?>
下一篇:PHP面向对象之旅:深入理解static变量与方法