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