本文实例为大家分享了php实现简单四则运算器的具体代码,供大家参考,具体内容如下
前端代码 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<!DOCTYPE html>
< html >
< head >
< meta charset = "UTF-8" >
< title >php计算器</ title >
</ head >
< body >
< form name = "form" method = "post" action = "phpjisuan.php" >
请输入num1:< input type = "text" name = "num1" ></ br ></ br >
请选择运算符:< select name = "sel" >
< option value = "+" >+</ option >
< option value = "-" >-</ option >
< option value = "*" >*</ option >
< option value = "/" >/</ option >
< option value = "%" >%</ option >
</ select ></ br ></ br >
请输入num2:< input type = "text" name = "num2" ></ br ></ br >
< input type = "submit" name = "Submit" value = "计算" >
< input type = "reset" name = "Reset" value = "重置" >
</ form >
</ body >
</ html >
|
后台:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
<!DOCTYPE html>
< html >
< head >
< meta charset = "UTF-8" >
< title >php计算器</ title >
<? php
$num1=$_POST["num1"];
$num2=$_POST["num2"];
$sel=$_POST["sel"];
?>
</ head >
< body >
<? php
if($sel=="+"){
$num=$num1+$num2;
}
else if($sel=="-"){
$num=$num1-$num2;
}
else if($sel=="*"){
$num=$num1*$num2;
}
else if($sel=="/"){
if($num2!=0){
$num=$num1/$num2;
}else{
echo "除数不能为0";
}
}
else if($sel=="%"){
if($num2!=0){
$num=$num1%$num2;
}else{
echo "除数不能为0";
}
}
if(isset($num)){
echo "运算结果为$num";
}
?>
</ body >
</ html >
|
效果图1:
效果图2:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_43144014/article/details/82811486