Archive:
1<! DOCTYPE html>234<title>php Implementation Calculator </title>56 7<body>8<?PHP9 $mess= "";Ten if(isset($_post["Sub"])){ One if($_post["num1"] = = ""){ A $mess. = "The first number cannot be empty!<br>"; - } - Else{ the if(!Is_numeric($_post["NUM1"])){ - $mess. = "The first number must be a digital!<br>"; - } - } + - if($_post["Num2"]== ""){ + $mess. = "The second number cannot be empty!<br>"; A } at Else{ - if(!Is_numeric($_post["Num2"])){ - $mess. = "The second number must be a number!<br>"; - } - Else{ - if($_post["Opt"]== "/" &&$_post["Num2"]==0){ in $mess. = "Divisor cannot be 0"; - } to } + } - } the?> *<table border= "1" align= "center" width= ">" $<form action= "" method= "POST" >Panax Notoginseng<caption> -<tr> the<td> +<input type= "Text" size= "4" name= "NUM1" value= "<?php Echo$_post["Num1"]?> "/> A</td> the +<td> -<select name= "opt" > $<option value= "+" <?phpEcho $_post["opt"]== "+"? "Selected": "?>>+</option> $<option value= "-" <?phpEcho $_post["Opt"]== "-"? "Selected": "?>>-</option> -<option value= "x" <?phpEcho $_post["Opt"]== "x"? "Selected": "?>>x</option> -<option value= "/" <?phpEcho $_post["Opt"]== "/"?) "Selected": "?>>/</option> the<option value= "%" <?phpEcho $_post["opt"]== "%"? "Selected": "?>>%</option> -</select>Wuyi</td> the -<td> Wu<input type= "Text" size= "4" name= "num2" value= "<?php Echo$_post["num2"]?> "/> -</td> About $<td> -<input type= "Submit" Name= "sub" value= "Calculation"/> -</td> -</tr> A</form> +<?PHP the if(isset($_post["Sub"])){ - Echo' <tr><td colspan= ' 4 "> '; $ if(!$mess){ the $sum=0; the Switch($_post["Opt"]){ the Case"+": the $sum=$_post["NUM1"] +$_post["Num2"]; - Break; in Case"-": the $sum=$_post["NUM1"]-$_post["Num2"]; the Break; About Case"X": the $sum=$_post["NUM1"] *$_post["Num2"]; the Break; the Case"/": + $sum=$_post["NUM1"]/$_post["Num2"]; - Break; the Case"%":Bayi $sum=$_post["NUM1"]%$_post["Num2"]; the Break; the } - - Echo"Result: {$_post[' Num1 ']} {$_post[' Opt ']} {$_post[' Num2 ']} = {$sum}"; the } the Else{ the Echo $mess; the } - Echo' </td></tr> '; the } the?> the</table>94</body> theThe results are as follows:
Using PHP to implement a simple home-made calculator