PHP's Base Calculator

Source: Internet
Author: User

Design a computational function that can perform operations and be able to validate unreasonable data and give false hints.

Rule: The first number, the second number cannot be empty

If the operator is/, the second number cannot be 0.

1<?PHP2 Header(' content-type:text/html; Charset=utf-8 ');3 /*Design a computational function that can perform operations and be able to validate unreasonable data and give false hints.4 rule: The first number, the second number cannot be empty5 If the operator is/, the second number cannot be 0.*/6 7 functionJSQ ($num 1,$oper,$num 2){8     //The detection data cannot be empty and prompt9     if(!isset($num 1) || !Is_numeric($num 1)){Ten         $error= <<<ERROR One<script> AAlert (' The first number is not legal '); -</script> - ERROR; the         return $error; -     } -     if(!isset($num 2) || !Is_numeric($num 2)){ -         $error= <<<ERROR +<script> -Alert (' The second number is not legal '); +</script> A ERROR; at         return $error; -     } -  -     if($oper== "+"){ -         $result=$num 1+$num 2; -}ElseIf($oper== "-"){ in         $result=$num 1-$num 2; -}ElseIf($oper== "*"){ to         $result=$num 1*$num 2; +}ElseIf($oper= "/"){ -         if($num 2= = 0){ the             $error= <<<ERROR *<script> $Alert (' The second number cannot be 0 '));Panax Notoginseng</script> - ERROR; the         return $error; +         } A         $result=$num 1/$num 2; the     } +     return $result; - } $  $ if($_server[' request_method '] = = "POST"){ -     $res= JSQ ($_post[' NUM1 '],$_post[' Oper '],$_post[' Num2 ']); - } the?> - Wuyi the<form action= "<?php Echo$_server[' php_self '];?> "method=" POST "> -First Number: <input type= "text" name= "NUM1"/><p> WuOperator: <select name= "Oper" > -<option value= "+" > + </option> About<option value= "-" >-</option> $<option value= "*" > * </option> -<option value= "/" >/</option> -</select><p> -Second number: <input type= "text" name= "num2"/><p> A<input type= "Submit" value= "calculation"/> +  the</form> -The result is: <?phpEcho isset($res)?$res: "";?>
View Code

PHP's Base Calculator

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: info-contact@alibabacloud.com 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.