PHP provides several arithmetic operators to perform mathematical operations on numeric values. These include:
Operator | Description | Example |
---|---|---|
+ |
Addition | $a + $b |
- |
Subtraction | $a - $b |
* |
Multiplication | $a * $b |
/ |
Division | $a / $b |
% |
Modulus (Remainder) | $a % $b |
** |
Exponentiation | $a ** $b |
1. Addition (+)
The addition operator (+
) is used to add two numbers.
<?php $a = 10; $b = 5; $sum = $a + $b; echo $sum; // Outputs: 15 ?>
2. Subtraction (-)
The subtraction operator (-
) is used to subtract one number from another.
<?php $a = 15; $b = 5; $difference = $a - $b; echo $difference; // Outputs: 10 ?>
3. Multiplication (*)
The multiplication operator (*
) is used to multiply two numbers.
<?php $a = 6; $b = 4; $product = $a * $b; echo $product; // Outputs: 24 ?>
4. Division (/)
The division operator (/
) divides one number by another.
<?php $a = 20; $b = 4; $quotient = $a / $b; echo $quotient; // Outputs: 5 ?>
5. Modulus (%)
The modulus operator (%
) returns the remainder of division.
<?php $a = 10; $b = 3; $remainder = $a % $b; echo $remainder; // Outputs: 1 ?>
6. Exponentiation (**)
The exponentiation operator (**
) raises a number to the power of another number.
<?php $a = 3; $b = 2; $power = $a ** $b; echo $power; // Outputs: 9 ?>
Conclusion
PHP provides powerful arithmetic operators to perform mathematical operations efficiently. Understanding these operators is essential for performing calculations in PHP scripts.