Arithmetic operators are used in PHP to perform basic mathematical operations like addition, subtraction, multiplication, division, and more. These operators allow you to work with numbers and perform calculations.
In this tutorial, we will cover the following arithmetic operators in PHP:
Table of Contents
We will explore each operator with code examples and explanations.
1. Addition (+)
The addition operator (+) is used to add two numbers.
Syntax:
$result = $a + $b;
Example:
<?php $a = 10; $b = 20; $result = $a + $b; echo "Addition: $a + $b = $result"; // Outputs: Addition: 10 + 20 = 30 ?>
In this example, $a and $b are added together, and the result is stored in the $result variable, which is then displayed.
2. Subtraction (-)
The subtraction operator (-) is used to subtract one number from another.
Syntax:
$result = $a - $b;
Example:
<?php $a = 30; $b = 10; $result = $a - $b; echo "Subtraction: $a - $b = $result"; // Outputs: Subtraction: 30 - 10 = 20 ?>
Here, $b is subtracted from $a, and the result is displayed.
3. Multiplication (*)
The multiplication operator (*) is used to multiply two numbers.
Syntax:
$result = $a * $b;
Example:
<?php $a = 5; $b = 4; $result = $a * $b; echo "Multiplication: $a * $b = $result"; // Outputs: Multiplication: 5 * 4 = 20 ?>In this case, $a and $b are multiplied, and the result is stored in $result.
4. Division (/)
The division operator (/) is used to divide one number by another.
Syntax:
$result = $a / $b;
Example:
<?php $a = 20; $b = 4; $result = $a / $b; echo "Division: $a / $b = $result"; // Outputs: Division: 20 / 4 = 5 ?>
In this example, $a is divided by $b, and the result is displayed.
Handling Division by Zero:
Attempting to divide by zero will cause an error. You should handle such cases properly.
Example:
<?php $a = 10; $b = 0; if ($b != 0) { $result = $a / $b; echo "Division: $a / $b = $result"; } else { echo "Error: Division by zero is not allowed!"; } // Outputs: Error: Division by zero is not allowed! ?>
This example prevents division by zero by checking if $b is zero before performing the division.
5. Modulus (%)
The modulus operator (%) returns the remainder of a division between two numbers.
Syntax:
$result = $a % $b;
Example:
Table of Contents
<?php $a = 10; $b = 3; $result = $a % $b; echo "Modulus: $a % $b = $result"; // Outputs: Modulus: 10 % 3 = 1 ?>
In this example, the modulus operator calculates the remainder of the division of $a by $b.
6. Exponentiation (**)
The exponentiation operator (**) raises one number to the power of another.
Syntax:
$result = $a ** $b;
Example:
<?php $a = 2; $b = 3; $result = $a ** $b; echo "Exponentiation: $a ** $b = $result"; // Outputs: Exponentiation: 2 ** 3 = 8 ?>
Here, $a is raised to the power of $b, and the result is displayed.
7. Increment (++)
The increment operator (++) increases the value of a variable by 1.
Syntax:
++$a; // Pre-increment $a++; // Post-increment
Example:
<?php $a = 5; echo "Before increment: $a<br>"; ++$a; // Pre-increment echo "After pre-increment: $a<br>"; // Outputs: After pre-increment: 6 $a = 5; echo "Before increment: $a<br>"; $a++; // Post-increment echo "After post-increment: $a<br>"; // Outputs: After post-increment: 6 ?>
Pre-increment (++$a): Increases the value of $a by 1 before using it.
Post-increment ($a++): Increases the value of $a by 1 after using it.
8. Decrement (–)
The decrement operator (–) decreases the value of a variable by 1.
Syntax:
--$a; // Pre-decrement $a--; // Post-decrement
Example:
<?php $a = 5; echo "Before decrement: $a<br>"; --$a; // Pre-decrement echo "After pre-decrement: $a<br>"; // Outputs: After pre-decrement: 4 $a = 5; echo "Before decrement: $a<br>"; $a--; // Post-decrement echo "After post-decrement: $a<br>"; // Outputs: After post-decrement: 4 ?>
Pre-decrement (–$a): Decreases the value of $a by 1 before using it.
Post-decrement ($a–): Decreases the value of $a by 1 after using it.
Combining Arithmetic Operators with Assignment (+=, -=, *=, etc.)
PHP allows you to combine arithmetic operations with assignment.
Syntax:
$a += $b; // Same as $a = $a + $b; $a -= $b; // Same as $a = $a - $b; $a *= $b; // Same as $a = $a * $b; $a /= $b; // Same as $a = $a / $b; $a %= $b; // Same as $a = $a % $b; $a **= $b; // Same as $a = $a ** $b;
Example:
<?php $a = 10; $a += 5; // Adds 5 to $a, now $a = 15 echo "a += 5: $a<br>"; // Outputs: a += 5: 15 $a = 20; $a -= 4; // Subtracts 4 from $a, now $a = 16 echo "a -= 4: $a<br>"; // Outputs: a -= 4: 16 $a = 2; $a **= 3; // Raises $a to the power of 3, now $a = 8 echo "a **= 3: $a<br>"; // Outputs: a **= 3: 8 ?>
Conclusion
PHP provides a wide range of arithmetic operators that allow you to perform mathematical calculations efficiently. By using these operators, you can add, subtract, multiply, divide, and work with more complex calculations like exponents and modulus.
Key Arithmetic Operators in PHP:
Addition (+): Adds two numbers.
Subtraction (-): Subtracts one number from another.
Multiplication (*): Multiplies two numbers.
Division (/): Divides one number by another.
Modulus (%): Returns the remainder of division.
Exponentiation (**): Raises a number to the power of another.
Increment (++): Increases the value of a variable by 1.
Decrement (–): Decreases the value of a variable by 1.
By practicing these operators, you’ll gain a solid understanding of how to work with numbers in PHP, which is essential for many programming tasks.