Arithmetic Operators in php
Purpose
- Arithmetic operators in php are used to perform mathematical calculations on numbers in PHP.
Common Arithmetic Operators:
- Addition (+): Adds two numbers together.
- $result = 10 + 5; // $result will be 15
- Subtraction (-): Subtracts the second number from the first.
- $result = 10 – 5; // $result will be 5
- Multiplication (*): Multiplies two numbers.
- $result = 10 * 5; // $result will be 50
- Division (/): Divides the first number by the second.
- $result = 10 / 2; // $result will be 5
- Modulus (%): Returns the remainder after division.
- $result = 10 % 3; // $result will be 1
- Exponentiation (**): Raises a number to the power of another.
- $result = 2 ** 3; // $result will be 8 (2 raised to the power of 3)
Example:
<?php
$num1 = 10;
$num2 = 5;
$sum = $num1 + $num2;
$difference = $num1 - $num2;
$product = $num1 * $num2;
$quotient = $num1 / $num2;
$remainder = $num1 % $num2;
$power = $num1 ** 2; // 10 raised to the power of 2
echo "Sum: " . $sum . "<br>";
echo "Difference: " . $difference . "<br>";
echo "Product: " . $product . "<br>";
echo "Quotient: " . $quotient . "<br>";
echo "Remainder: " . $remainder . "<br>";
echo "Power: " . $power . "<br>";
?>
This code will output:
Sum: 15
Difference: 5
Product: 50
Quotient: 2
Remainder: 0
Power: 100