PHP Lesson 7 – Arithmetic Operators

Last updated on December 9th, 2023

What are arithmetic operators in PHP?

Arithmetic operators are used to perform basic mathematical operations in the PHP scripts – addition, subtraction, multiplication, division, etc.

What types of arithmetic operators are there?

There are 5 types of arithmetic operators:

1. Addition  Operator (+)
2. Subtraction Operator ()
3. Multiplication Operator (*)
4. Division Operator (/)
5. Modulus Operator (%)

Examples of using arithmetic operators

1. Addition (+) Operator:

<?php
  $num1 = 5;
  $num2 = 3;

  $sum = $num1 + $num2;
  echo "Sum: $sum";
?>

Result: The script will display “Sum: 8.”

In this example, the + operator is used to add the values of $num1 (5) and $num2 (3) together, resulting in a sum of 8.

2. Subtraction (-) Operator:

<?php
  $num1 = 10;
  $num2 = 4;

  $difference = $num1 - $num2;
  echo "Difference: $difference";
?>

Result: The script will display “Difference: 6.”

Here, the – operator subtracts the value of $num2 (4) from the value of $num1 (10), resulting in a difference of 6.

3. Multiplication (*) Operator:

<?php
  $num1 = 6;
  $num2 = 7;

  $product = $num1 * $num2;
  echo "Product: $product";
?>

Result: The script will display “Product: 42.”

In this script, the * operator multiplies the values of $num1 (6) and $num2 (7), resulting in a product of 42.

4. Division (/) Operator:

<?php
  $numerator = 20;
  $denominator = 5;

  $quotient = $numerator / $denominator;
  echo "Quotient: $quotient";
?>

Result: The script will display “Quotient: 4.”

This example uses the / operator to divide the value of $numerator (20) by the value of $denominator (5), resulting in a quotient of 4.

5. Modulus (%) Operator:

<?php
  $num1 = 10;
  $num2 = 3;

  $remainder = $num1 % $num2;
  echo "Remainder: $remainder";
?>

Result: The script will display “Remainder: 1.”

Here, the % operator calculates the remainder when $num1 (10) is divided by $num2 (3), which is 1 in this case. For example, if we change the first number ($num1) to 100 and the second ($num2) to 50, then the result will be zero (Reminder: 0).

What are increment and decrement operators?

In PHP, the increment and decrement operators are used to increase or decrease the value of a variable by 1, respectively. They are handy shortcuts for common operations.

Increment Operator (++)
The increment operator (++) adds 1 to the variable’s current value.
It can be used as a prefix (++$variable) or postfix ($variable++) operator.
Example:

<?php
$count = 5;
++$count; // $count is now 6
?>

In the above example, the value of $count is incremented by 1 using the prefix increment operator (5+1=6).

Decrement Operator (–)
The decrement operator (–) subtracts 1 from the variable’s current value.Like the increment operator, it can be used as a prefix (–$variable) or postfix ($variable–) operator.
Example:

<?php
$score = 10;
$score--; // $score is now 9
?>

Here, the value of $score is decremented by 1 using the postfix decrement operator (10-1=9).

These operators are particularly useful in loops and other situations where you need to update a variable’s value by a fixed amount.

Importance of operator position
Remember, when used in expressions, the position of the operator can affect how the variable is evaluated. For example:

<?php
$x = 5;
$y = ++$x;
// $x is first incremented, then $y is assigned the new value of $x (6 in this case)
?>

Contrast this with:

<?php
$x = 5;
$y = $x++;
// $y is first assigned the current value of $x, then $x is incremented (resulting in $y = 5, $x = 6)
?>

Are there operators with priority over others?

Regarding precedence with arithmetic operators, there are two simple rules to remember for now:
– the operations enclosed in parentheses are of the highest priority for arithmetic operators;
– multiplication and division have priority over addition and subtraction.

Example:

<?php
$calc = 2 + 3 * 6;
echo "The result is $calc"; // The result will be 20
?>

The above code will return a result of 20 because the multiplication of 3*6=18 is performed first, then 2 is added to 18.

To perform the addition first and then the multiplication, the above code should be written like this:

<?php
$calc = (2 + 3) * 6;
echo "The result is $calc"; // The result will be 30
?>

Now the addition operation (2+3=5) is performed first because the expression in the parentheses has the highest priority in execution, then 5 is multiplied by 6 and the final result is 30.