PHP Basics
Functions in PHP
Working with Forms
Working with Files
Working with Databases
Advanced PHP Techniques

Arithmetic operators in PHP are used to perform common arithmetic operations, such as addition, subtraction, multiplication, division, and modulus. Here’s a deep dive into each operator, including their syntax, usage, and examples:

1. Addition (+)

Description: Adds two numbers together.

Syntax:

				
					$result = $a + $b;

				
			

Example:

				
					$a = 5;
$b = 3;
$result = $a + $b;  // $result is 8

				
			

2. Subtraction (-)

Description: Subtracts one number from another.

Syntax:

				
					$result = $a - $b;

				
			

Example:

				
					$a = 5;
$b = 3;
$result = $a - $b;  // $result is 2

				
			

3. Multiplication (*)

Description: Multiplies two numbers.

Syntax:

				
					$result = $a * $b;

				
			

Example:

				
					$a = 5;
$b = 3;
$result = $a * $b;  // $result is 15

				
			

4. Division (/)

Description: Divides one number by another.

Syntax:

				
					$result = $a / $b;

				
			

Example:

				
					$a = 6;
$b = 3;
$result = $a / $b;  // $result is 2

				
			

Note: Be cautious of division by zero, as it will produce an error.

5. Modulus (%)

Description: Returns the remainder of the division of one number by another.

Syntax:

				
					$result = $a % $b;

				
			

Example:

				
					$a = 5;
$b = 3;
$result = $a % $b;  // $result is 2

				
			

6. Exponentiation (**)

Description: Raises one number to the power of another.

Syntax:

				
					$result = $a ** $b;

				
			

Example:

				
					$a = 2;
$b = 3;
$result = $a ** $b;  // $result is 8

				
			

Increment/Decrement Operators

These operators are not strictly arithmetic operators, but they are closely related and often used in arithmetic expressions.

7. Increment (++)

Description: Increases an integer value by one.

Syntax:

				
					$a++;

				
			

Example:

				
					$a = 5;
$a++;  // $a is now 6

				
			

8. Decrement (--)

Description: Decreases an integer value by one.

Syntax:

				
					$a--;

				
			

Example:

				
					$a = 5;
$a--;  // $a is now 4

				
			

Combining Arithmetic Operators with Assignment (+=, -=, *=, /=, %=, **=)

These combined operators perform an arithmetic operation and an assignment in one step.

Syntax:

				
					$a += $b;  // Equivalent to $a = $a + $b;
$a -= $b;  // Equivalent to $a = $a - $b;
$a *= $b;  // Equivalent to $a = $a * $b;
$a /= $b;  // Equivalent to $a = $a / $b;
$a %= $b;  // Equivalent to $a = $a % $b;
$a **= $b; // Equivalent to $a = $a ** $b;

				
			

Example:

				
					$a = 5;
$b = 3;
$a += $b;  // $a is now 8
$a -= $b;  // $a is now 5
$a *= $b;  // $a is now 15
$a /= $b;  // $a is now 5
$a %= $b;  // $a is now 2
$a **= $b; // $a is now 8

				
			

Operator Precedence

Operator precedence determines the order in which operations are performed. In PHP, arithmetic operators have the following precedence (from highest to lowest):

  1. **
  2. *‘, ‘/‘, ‘%
  3. +‘, ‘-

Example:

				
					$result = 5 + 3 * 2;  // $result is 11, because * has higher precedence than +

				
			

Associativity

Associativity determines the direction in which operations of the same precedence are performed. Most arithmetic operators are left-associative (evaluated from left to right), except for **, which is right-associative.

Example:

				
					$result = 2 ** 3 ** 2;  // $result is 512, because ** is right-associative

				
			

Understanding these arithmetic operators in PHP helps in performing a wide range of mathematical computations effectively. Combining them with other PHP features allows for the creation of complex and dynamic applications.

Scroll to Top