PHP Arithmetic Operators

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
?>

Try It Now

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
?>

Try It Now

3. Multiplication (*)

The multiplication operator (*) is used to multiply two numbers.

<?php
   $a = 6;
   $b = 4;
   $product = $a * $b;

   echo $product; // Outputs: 24
?>

Try It Now

4. Division (/)

The division operator (/) divides one number by another.

<?php
    $a = 20;
    $b = 4;
    $quotient = $a / $b;

    echo $quotient; // Outputs: 5
?>

Try It Now

5. Modulus (%)

The modulus operator (%) returns the remainder of division.

<?php
    $a = 10;
    $b = 3;
    $remainder = $a % $b;

    echo $remainder; // Outputs: 1
?>

Try It Now

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
?>

Try It Now

Conclusion

PHP provides powerful arithmetic operators to perform mathematical operations efficiently. Understanding these operators is essential for performing calculations in PHP scripts.