Using PHP to implement a simple home-made calculator

Source: Internet
Author: User


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> the

The results are as follows:

Using PHP to implement a simple home-made calculator

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.