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

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

小老鼠
Release: 2023-04-14 09:51:55
Original
3308 people have browsed it

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

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
OperatorNameDescriptionInstanceResult
x yAdd the sum of x and y2 24
x - yminus the difference between x and y5 - 23
DivideQuotient of x and y15 / 53
Modulo (remainder of division)Remainder of x divided by y5 % 210 % 2

1
0

Set a negative numberTake the opposite sign of x
<?php
$x = 2;echo -$x;?>
Copy after login
-2
Inversion
~1=-2;   ~0=-1;
Copy after login

<?php
$x = 2;echo ~$x;?>
Copy after login
-3
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!

Related labels:
php
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