PHP is one of the common high-level programming languages used for web development. PHP has many advantages compared to other programming languages, one of which is that it is easy to learn. In this article, we will use PHP to implement a calculator for the four arithmetic operations of addition, subtraction, multiplication, and division.
1. Environment preparation
Before writing code, we need to ensure that the PHP running environment has been installed on the computer. If you haven't installed it yet, you can go to the official website to download the latest version of the PHP installer, and then follow the prompts to install it.
2. Development Ideas
Before implementing the calculator, we need to plan the development idea of the program:
3. HTML form
First, we need to create a form in HTML, the code is as follows:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>计算器</title> </head> <body> <h1>计算器</h1> <form method="post" action="calculator.php"> <label for="num1">第一个数字:</label> <input type="number" id="num1" name="num1" required> <br> <label for="num2">第二个数字:</label> <input type="number" id="num2" name="num2" required> <br> <label for="operator">选择运算符:</label> <select id="operator" name="operator"> <option value="+">加</option> <option value="-">减</option> <option value="*">乘</option> <option value="/">除</option> </select> <br> <input type="submit" value="计算"> </form> </body> </html>
In the above code, we create a form through HTML form. The form is submitted to the calculator.php page through the POST method, which contains two numeric input boxes and a selection box, and the user selects the calculation operation that needs to be performed.
4. PHP Calculator Implementation
Next, we need to go to the PHP file calculator.php to complete the specific implementation of the calculator.
Before starting, first get the value submitted by the form:
$num1 = $_POST['num1']; $num2 = $_POST['num2']; $operator = $_POST['operator'];
Then, we need to perform the corresponding calculation operation:
switch($operator) { case '+': $result = $num1 + $num2; break; case '-': $result = $num1 - $num2; break; case '*': $result = $num1 * $num2; break; case '/': $result = $num1 / $num2; break; }
In the above code, we use the switch statement To perform corresponding calculation operations based on the value of the selection box, calculate the results of addition, subtraction, multiplication, and division respectively, and the results are saved in the variable $result.
Finally, we need to output the calculation results to the page:
echo "<h1>计算结果</h1>"; echo "<p>{$num1} {$operator} {$num2} = {$result}</p>";
The above code uses the echo keyword to output the calculation results to the page, and the user can clearly see the calculation results.
5. Complete code
The following is the complete calculator.php code:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>计算器</title> </head> <body> <h1>计算器</h1> <form method="post" action="calculator.php"> <label for="num1">第一个数字:</label> <input type="number" id="num1" name="num1" required> <br> <label for="num2">第二个数字:</label> <input type="number" id="num2" name="num2" required> <br> <label for="operator">选择运算符:</label> <select id="operator" name="operator"> <option value="+">加</option> <option value="-">减</option> <option value="*">乘</option> <option value="/">除</option> </select> <br> <input type="submit" value="计算"> </form> <?php if ($_POST) { $num1 = $_POST['num1']; $num2 = $_POST['num2']; $operator = $_POST['operator']; switch($operator) { case '+': $result = $num1 + $num2; break; case '-': $result = $num1 - $num2; break; case '*': $result = $num1 * $num2; break; case '/': $result = $num1 / $num2; break; } echo "<h1>计算结果</h1>"; echo "<p>{$num1} {$operator} {$num2} = {$result}</p>"; } ?> </body> </html>
6. Summary
Through this article, we learned how A calculator that uses PHP language to implement the four arithmetic operations of addition, subtraction, multiplication and division. During the implementation, we first created an HTML form where the user could enter numbers and select operators. Next, we use PHP to obtain the value submitted by the form and perform corresponding calculation operations. Finally, we output the calculation results to the page so that users can clearly see it.
The above is the detailed content of How to implement addition, subtraction, multiplication and division calculations in php. For more information, please follow other related articles on the PHP Chinese website!