The arithmetic in PHP means all number types that can be calculated through the common arithmetic operators such as multiplication, addition, subtraction, division, modulus, and the exponentiation.
Table Of Contents
In the following table you will see all PHP arithmetic operators.
|+=||Addition||To sum two numbers together.|
|-=||Subtraction||To minus two numbers|
|*=||Multiplication||To multiply numbers together|
|/=||Division||To divide two digits|
|%=||Modulus||The first variable is divided by the second one.|
|**=||Exponentiation||Refers to the calculated exponentiation|
In the following sections, I will explain each one in-depth, Let’s dive right in.
The PHP addition operator used to sum two or more numbers together. So that means calculate two values and extract the sum of these values.
In other words, the addition operator appears between numbers with this “+” symbol, that to allow the two values expose the final sum result.
<?php $number1 = 10; $number2 = 20; $sum = $number1 + $number2; echo $sum; // 30 ?>
In another example you will see addition with equal operator.
<?php $i = 10; $i += 11; echo $i // 21 ?>
To clarify that more, Use the addition operator with many numbers.
<?php $value = 10 + 20 + 50 + 100; echo $value; // 180 ?>
In the following section, I will explain the subtraction operator.
The PHP subtraction, used to subtract number from a another number else. So it takes this symbol “-”.Let’s see an example.
<?php $number1 = 10; $number2 = 20; $calc = $number2 - $number1; echo $calc; // 20 ?>
Or it can be used with the equal operator, like the below code.
<?php $i = 100; $i -= 45; echo $i; // 55 ?>
Let’s move to the multiplication operator.
The PHP multiplication concept means to multiply two or more numbers together which means multiply a specific number on another number else and It takes this symbol ”*“.
The following is an example for the common multiplication operator
<?php $number_a = 20; $number_b = 5; echo $number_a * $number_b; // 100 ?>
Use the equal operator with the multiplication operator.
<?php $number_a = 20; $number_a *= 5; echo $number_a; // 100 ?>
Anyway, Let’s move to the division operator.
This operator used to divide two numbers together. It takes this symbol “/“. Let’s see an example.
<?php $number_a = 20; $number_b = 5; echo $number_a / $number_b; // 4 ?>
In the following example, I used the equal operator with the division operator
<?php $number_a = 20; $number_a /= 5; echo $number_a; // 4 ?>
The PHP modulus operator takes this symbol “%” between two numbers. This operator used to ignore count of many of a specific number inside another number.
If 3 is counted for 3 times inside 10, so the answer will be 1.
If 5 is counted for 4 times inside 20, so the answer will be 0.
Let’s see a real example with PHP.
<?php $number = 105 % 25; // => the 25 is counted for only 4 times inside the 105 so the answer is 5 echo $number; // 5 ?>
The PHP exponentiation operator means double multiplication in the same number. So, if the exponent of the 10 number is 5, the result would be 1010 = 100000
Let’s see that in PHP code.
<?php $number = 10 ** 2; // => 10 x 10 echo $number; // 100 ?>
In the following example, you will use the exponentiation with equal operator.
<?php $number = 10; $number **= 2; // => 10 x 10 echo $number; // 100 ?>
In this article, you understood the type of PHP arithmetic operators such as the following list.
To learn more about it, visit PHP manual.