用php简单实现加减乘除计算器
程序员文章站
2023-11-16 23:25:52
用php实现加减乘除计算器。代码很简单哦! 复制代码 代码如下:
用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'];
?>