1 <html>
2 <head>
3 <meta charset="utf-8" />
4 <title>计算器</title>
5 </head>
6 <body>
7 <form action="" method="post">
8 <input type="text" name="one" /><br />
9 <input type="radio" name="suan" value="+" />+
10 <input type="radio" name="suan" value="-" />-
11 <input type="radio" name="suan" value="*" />*
12 <input type="radio" name="suan" value="/" />/<br />
13 <input type="text" name="two" /><br />
14 <input type="submit" value="提交" />
15 </form>
16 </body>
17 </html>
18
19 <?php
20 //var_dump($_POST);exit();
21 if(!empty($_POST['one']) && !empty($_POST['two']) && !empty($_POST['suan'])){
22 $one = $_POST['one'];
23 $two = $_POST['two'];
24 $suan = $_POST['suan'];
25 //var_dump($_POST);exit();
26 //echo "$one$suan$two";exit();
27 if($suan == "+"){
28 echo "$one$suan$two=".($one+$two);
29 }elseif($suan == "-"){
30 echo "$one$suan$two=".($one-$two);
31 }elseif($suan == "*"){
32 echo "$one$suan$two=".$one*$two;
33 }else{
34 echo "$one$suan$two=".$one/$two;
35 }
36 }
【注】上面放这些代码只是为了想让我注意一个问题,就是运算符的优先级问题,做加减法是老是输出不了前面的字符串echo "$one$suan$two=".($one+$two);,
乘除运算就可以,弄了不少时间,终于测试了知道加减法相对PHP的"."点号优先级较低,所以只要给运算加个括号即可正常显示输出。