PHP Arithmetic Operators

Last updated on

PHP performs mathematical operations using arithmetic operators with numeric values, variables, and expressions.

Table of Contents

There are six arithmetic operators in PHP which are in below table:

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

Before reading this tutorial, you have to know what link is and understand the link types. You can learn more about it through this link.

Addition Operator

The addition operator in PHP is represented by the symbol “+”. It is used to add two or more numbers together. Here’s an example:

<?php
  $number1 = 10;
  $number2 = 4;
  $result = $number1 + $number2;
  echo $result; // Output: 14
?>

In this example, we added the values of $number1 (which is 10) and $number2 (which is 4) using the addition operator, and stored the resulting value of 14 in the variable $result.

Finally, we outputted the value of $result to the screen using the echo statement.

To further elaborate, employ the addition operator to multiple numbers.

<?php
  $value = 10 + 20 + 50 + 100;
  echo $value; // 180
?>

The addition operator can also be used with the equal operator to perform an addition and assign the result to a variable in one step. So, Here’s an example:

<?php
  $number1 = 10;
  $number2 = 4;
  $number1 += $number2;
  echo $number1; // Output: 14
?>

In this example, we used the addition operator with the equal operator to add the value of $number2 (which is 4) to the value of $number1 (which is 10), and then assigned the resulting value of 14 back to $number1 in one step. Finally, we outputted the new value of $number1 to the screen using the echo statement.

Subtraction Operator

PHP’s subtraction operator is represented by the symbol “-“, and it is used to subtract one number from another. Here’s an example:

<?php
  $number1 = 10;
  $number2 = 4;
  $calc    = $number1 - $number2 ;
  echo $calc; // 6
?>

In this example, we subtracted the value of $number2 (which is 4) from the value of $number1 (which is 10) using the subtraction operator. The resulting value of 6 is then assigned to the variable $calc. Which is then outputted to the screen using the echo statement.

The subtraction operator can also be used with the equal operator to perform a subtraction and assign the result to a variable in one step. Here’s an example:

<?php
  $number1 = 10;
  $number2 = 4;
  $number1 -= $number2;
  echo $number1; // Output: 6
?>

We used the subtraction operator with the equal operator to subtract the value of $number2 (4) from $number1 (10) and assigned the resulting value of 6 back to $number1 in one step. Then, we outputted the new value of $number1 to the screen using the echo statement.

Multiplication Operator

The multiplication operator in PHP is represented by the symbol “*”. It is used to multiply two or more numbers together. Here’s an example:

<?php
  $number1 = 10;
  $number2 = 4;
  $result = $number1 * $number2;
  echo $result; // Output: 40
?>

In this example, we multiplied the values of $number1 (which is 10) and $number2 (which is 4) using the multiplication operator and stored the resulting value of 40 in the variable $result. Finally, we outputted the value of $result to the screen using the echo statement.

The multiplication operator can also be used with the equal operator to perform a multiplication and assign the result to a variable in one step. Here’s an example:

<?php
 $number1 = 10;
 $number2 = 4;
 $number1 *= $number2;
 echo $number1; // Output: 40
?>

In this example, we used the multiplication operator with the equal operator to multiply the value of $number1 (which is 10) by the value of $number2 (which is 4), and then assigned the resulting value of 40 back to $number1 in one step. Finally, we outputted the new value of $number1 to the screen using the echo statement.

Division Operator

The division operator in PHP is represented by the symbol “/”. It is used to divide one number by another. Here’s an example:

<?php
  $number1 = 10;
  $number2 = 4;
  $result = $number1 / $number2;
  echo $result; // Output: 2.5
?>

In this example, we divided the value of $number1 (which is 10) by the value of $number2 (which is 4) using the division operator, and stored the resulting value of 2.5 in the variable $result. Finally, we outputted the value of $result to the screen using the echo statement.

The division operator can also be used with the equal operator to perform a division and assign the result to a variable in one step. Here’s an example:

<?php
 $number1 = 10;
 $number2 = 4;
 $number1 /= $number2;
 echo $number1; // Output: 2.5
?>

In this example, we used the division operator with the equal operator to divide the value of $number1 (which is 10) by the value of $number2 (which is 4), and then assigned the resulting value of 2.5 back to $number1 in one step. Finally, we outputted the new value of $number1 to the screen using the echo statement.

Modulus Operator

Additionally, the modulus operator in PHP is represented by the symbol “%”. It is used to find the remainder when one number is divided by another. Here’s an example:

<?php
  $number1 = 10;
  $number2 = 4;
  $result = $number1 % $number2;
  echo $result; // Output: 2
?>

In this example, we used the modulus operator to find the remainder when the value of $number1 (which is 10) is divided by the value of $number2 (which is 4). The quotient is 2 with a remainder of 2, and the remainder is stored in the variable $result. Finally, we outputted the value of $result to the screen using the echo statement.

The modulus operator can also be used with the equal operator to perform a modulus operation and assign the result to a variable in one step. Here’s an example:

<?php
  $number1 = 10;
  $number2 = 4;
  $number1 %= $number2;
  echo $number1; // Output: 2
?>

In this example, we used the modulus operator with the equal operator to find the remainder when the value of $number1 (which is 10) is divided by the value of $number2 (which is 4), and then assigned the resulting remainder of 2 back to $number1 in one step. Finally, we outputted the new value of $number1 to the screen using the echo statement.  

Exponentiation Operator

The exponentiation operator in PHP is represented by the symbol “**”. It is used to raise a number to a power. Here’s an example:

<?php
  $number1 = 2;
  $number2 = 3;
  $result = $number1 ** $number2;
  echo $result; // Output: 8
?>

In this example, we used the exponentiation operator to raise the value of $number1 (which is 2) to the power of the value of $number2 (which is 3). The resulting value of 8 is stored in the variable $result. Finally, we outputted the value of $result to the screen using the echo statement.

The exponentiation operator can also be used with the equal operator to perform an exponentiation operation and assign the result to a variable in one step. Here’s an example:

<?php
  $number1 = 2;
  $number2 = 3;
  $number1 **= $number2;
  echo $number1; // Output: 8
?>

In this example, we used the exponentiation operator with the equal operator to raise the value of $number1 (which is 2) to the power of the value of $number2 (which is 3), and then assigned the resulting value of 8 back to $number1 in one step. Finally, we outputted the new value of $number1 to the screen using the echo statement.

Wrapping Up

In conclusion, PHP provides a variety of mathematical operators to perform arithmetic operations on numbers.

Moreover, the addition operator is represented by “+”, the subtraction operator is represented by “-“, the multiplication operator is represented by “*”, the division operator is represented by “/”, the modulus operator is represented by “%”, and the exponentiation operator is represented by “**”.

These operators can be used in various ways, including performing arithmetic operations on variables and assigning the results to new variables, as well as performing arithmetic operations and updating the value of existing variables in a single step using the equal operator.

By utilizing these operators, developers can perform a wide range of mathematical calculations and manipulate numeric data in their PHP programs.

To learn more about it, visit PHP manual.

Share on: