Home >Backend Development >PHP Problem >How to implement addition, subtraction, multiplication and division operations in php
php method to implement addition, subtraction, multiplication and division: 1. Create a php sample file; 2. Define two variables as "$x" and "$y"; 3. Through "$x $y", "$x-$y", "$x*$y" and "$x/$y" formulas implement addition, subtraction, multiplication and division operations; 4. Use "echo" to output the operation results.
This tutorial operating system: Windows 10 system, PHP8.1 version, Dell G3 computer
How to implement addition, subtraction, multiplication and division in php Operation?
In PHP, operators are divided into: arithmetic operators, logical operators, ternary operators and bitwise operators.
We can implement addition, subtraction, multiplication and division operations through arithmetic operators in PHP.
The calculation formulas for addition, subtraction, multiplication and division are "x y", "x - y", "x * y" and "x / y", which respectively represent the sum, difference, product and quotient of two numbers.
php implementation code is as follows:
<?php $x = 100; $y = 150; echo ($x + $y)."<br>"; // 输出 250 echo ($x - $y)."<br>"; // 输出 -100 echo ($x * $y)."<br>"; // 输出 1500 echo ($x / $y)."<br>"; // 输出 0.6666666666667 echo ($x % $y); // 输出 1 ?>
Output result:
PHP arithmetic operator:
Operator | Name | Description | Instance | Result |
---|---|---|---|---|
x y | Add the sum of | x and y | 2 2 | 4 |
x - y | minus the difference between | x and y | 5 - 2 | 3 |
multiplied by the product of | x and y | 5 * 2 | 10 | |
Divide | Quotient of x and y | 15 / 5 | 3 | |
Modulo (remainder of division) | Remainder of x divided by y | 5 % 2 | 10 % 810 % 2 1 | 20 |
Set a negative number | Take the opposite sign of x | <?php $x = 2;echo -$x;?> |
-2 | |
Inversion | x Inversion, perform "inversion" operation based on binary digits . Operation rules: | ~1=-2; ~0=-1; <?php $x = 2;echo ~$x;?> |
-3 | ##a . b |
Connect two strings | "Hi" . "Ha" | HiHa | ##Related expansion: |
Description | x = y | |
---|---|---|
The left operand is set to the value of the right-hand expression | x = y | |
Add | x -= y | |
minus | x *= y | |
times | x /= y | |
Division | x %= y | |
modulo (remainder of division) | a .= b | |
Connect two strings | Recommended learning: 《 |
The above is the detailed content of How to implement addition, subtraction, multiplication and division operations in php. For more information, please follow other related articles on the PHP Chinese website!