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.

Before getting started, you have to know what PHP integer is and understand the PHP INT types. You can learn more about it through this link.

PHP Arithmetic Operators

In the following table you will see all PHP arithmetic operators.

OperatorNameDescripion
+=AdditionTo sum two numbers together.
-=SubtractionTo minus two numbers
*=MultiplicationTo multiply numbers together
/=DivisionTo divide two digits
%=ModulusThe first variable is divided by the second one.
**=ExponentiationRefers to the calculated exponentiation

In the following sections, I will explain each one in-depth, Let’s dive right in.

Addition Operator

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.

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.

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.

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
?>

Modulus Operator

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
?>

Exponentiation Operator

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
?>

Wrapping Up

In this article, you understood the type of PHP arithmetic operators such as the following list.

  • Multiplication,
  • Addition,
  • Subtraction,
  • Division,
  • Modulus
  • Exponentiation

To learn more about it, visit PHP manual.