Home  >  Article  >  Backend Development  >  How to implement addition, subtraction, multiplication and division operations in php

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

小老鼠
小老鼠Original
2023-04-13 17:46:083247browse

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.

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

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:

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

PHP arithmetic operator:

##x * ymultiplied by the product of x and y5 * 210##x / yx % y 10 % 82-x~xx Inversion, perform "inversion" operation based on binary digits . Operation rules: ##a . bCollocation Connect two strings"Hi" . "Ha"HiHa##Related expansion:at In PHP, the basic assignment operator is =. It means that the left operand is set to the value of the right-hand expression. That is, the value of $x = 5 is 5.
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
Divide Quotient of x and y 15 / 5 3
Modulo (remainder of division) Remainder of x divided by y 5 % 210 % 2

1
0

Set a negative number Take the opposite sign of x
<?php
$x = 2;echo -$x;?>
-2
Inversion
~1=-2;   ~0=-1;

<?php
$x = 2;echo ~$x;?>
-3

Operator is equivalent to x = yx = x yx = x - yx = x * yx = x / y x = x % ya = a . bRecommended learning: 《PHP Video Tutorial》
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

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!

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