Home > Backend Development > PHP Problem > How to implement addition, subtraction, multiplication and division calculations in php

How to implement addition, subtraction, multiplication and division calculations in php

PHPz
Release: 2023-04-05 15:50:02
Original
2152 people have browsed it

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:

  1. First, we need to create a A form containing two numeric input boxes and a select box to determine the operator type.
  2. Next, we need to obtain the value submitted by the form through PHP and perform corresponding calculation operations.
  3. Finally, we need to output the calculation results to the page and present them on the page.

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>
Copy after login

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'];
Copy after login

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;
}
Copy after login

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>";
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template