Basic calculator for PHP, basic calculator for PHP

Design a calculation function that can complete calculations and verify unreasonable data and give error prompts.
Rule: The first number and the second number cannot be empty
If the operator is /, the second number cannot be 0.

1 php
2 header('Content-Type: text/html; charset=utf-8'
);
3 /*Design a calculation function that can complete operations and verify unreasonable data and give error prompts.
4 Rule: The first number and the second number cannot be empty
5 If the operator is /, the second count cannot be 0.*/
6
7 function jsq(
$num1,
$oper,
$num2){
8 //The detection data cannot be empty and prompts
9 if(!
isset(
$num1) || !
is_numeric(
$num1)){
10 $error = <<<
ERROR
11 <script>
<span>12</span> alert('The first number is illegal'<span>);
</span><span>13</span> </script>
14 ERROR;
15 return $error;
16 }
17 if(!
isset(
$num2) || !
is_numeric(
$num2)){
18 $error = <<<
ERROR
19 <script>
<span>20</span> alert('The second number is illegal'<span>);
</span><span>21</span> </script>
22 ERROR;
23 return $error;
24 }
25
26 if(
$oper == "+"
){
27 $result =
$num1 +
$num2;
28 }
elseif(
$oper == "-"
){
29 $result =
$num1 -
$num2;
30 }
elseif(
$oper == "*"
){
31 $result =
$num1 *
$num2;
32 }
elseif(
$oper = "/"
){
33 if(
$num2 == 0
){
34 $error = <<<
ERROR
35 <script>
<span>36</span> alert('The second number cannot be 0'<span>);
</span><span>37</span> </script>
38 ERROR;
39 return $error;
40 }
41 $result =
$num1 /
$num2;
42 }
43 return $result;
44 }
45
46 if(
$_SERVER['REQUEST_METHOD'] == "POST"
){
47 $res = jsq(
$_POST['num1'],
$_POST['oper'],
$_POST['num2'
]);
48 }49 ?>
50
51 Basic calculator written in php
52
64 The calculated result is: echo
isset(
$res)?
$res:" "; ?>
View Code
After php obtains an expression,
analyzes and processes the expression. You can read books on data structures.
If you are just doing a simple calculator, it is to get two numbers and one operator. That would be simpler.
I don’t know how many functions your calculator needs to achieve
Based on your code, I implemented it.
You can give it a try and ask if you have any questions.
Form data transfer Please enter two numbers in the form below to find their value
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn