Airthmetic Operators

Arithmetic Operators are used to perform mathematical operations e.g. addition operator (+), subtractaction operator (-), multiplication operator (*), division operator (/) and modulus operator (%). Let's go through all operators one by one.

Addition Operator (+)

This operator can be used to add one value into another value like numbers, string, arrays and hash tables.

Example
$mathsMarks = 81;
$scienceMarks = 93;

$totalMarks = $mathsMarks + $scienceMarks;

$totalMarks;

Above statements will give output total marks "174".

Let's take another example.

Example
$mathsMarks = 81;
$scienceMarks = 93;
$label = "Total Marks";
$totalMarks = $mathsMarks + $scienceMarks;

$label + $totalMarks;

Above statements will give output "Total Marks174".

Note: Here $label is a string type variable and if you use + operator with string type variable as a left operand then it will treat other variables as a string and it will concate two variables as string.

Subtractaction Operator (-)

This operator can be used to subtract one numeric value from another numeric value.

Example
$grossSalary = 10000;
$tax = 200;
$netSalary = $grossSalary - $tax

$netSalary;

Above statements will give output "9800" as net salary.

Multiplication Operator (*)

This operator can be used to multiply two numeric values.

Example
$grossSalary = 10000;
$months = 12;
$anualSalary = $grossSalary * $months

$anualSalary;

Above statements will give output "120000" as annual salary.

Division Operator (/)

This operator can be used to divide one value by another value.

Example
$anualSalary = 120000;
$months = 12;
$grossSalaryPerMonth = $anualSalary * $months

$grossSalaryPerMonth;

Above statements will give output "10000" as salary per month.

Modulus Operator (%)

This operator can be used to find modulus.

Example
$year = 2020;
$leapYear = $year % 4;

$leapYear;

Above statements will give output "0" because 2020 is a leap year.

 

← Previous

Next →