PHP Arithmetic Operators

Alike any other programming language or scripting language, In PHP, we can also perform arithmetic operations. Arithmetic operations such as addition, multiplication, division, subtraction, modulus operation. Lets see one by one.

Operation Operator Example
Addition Addition Operator(+) Addition of 5 and 6=11
Subtraction Subtraction Operator(-) Subtraction of 10 and 5=5
Multiplication Multiplication Operator(*) Multiplication of 5 and 6=30
Division Division Operator(/) Division of 10 by 5=2
Modulus Modulus Operator(%) Modulus of 12 by 10=2

Addition Operation using PHP:

To perform addition operation we use addition operator (+)

Example:

Lets add two numbers and print its value.

<?php $num1=10;

$num2=20;

echo $num1+$num2; ?>

Copy the code into index file, save it and run it. The webpage would show 30.

Lets take another example.

<?php $num1=20.22;

$num2=30.33;

echo $num1+$num2; ?>

Copy the above code into index file and save it. Run it. It would give 50.55.

The point here is, just by assigning value, we can perform addition operation. In our first example we have assigned integer value. But in second example we have assigned decimal value. In both case, we didn’t require any separate information to tell what should be the output(Unlike in any other programming language JAVA, C, C++). PHP engine going to understand from the assigned value what kind of data (whether its integer value or decimal value) is assigned. After addition operation, from the result value, it is going to understand, what type value is the result and it is going to print accordingly.

Subtraction Operation using PHP:

To perform subtraction operation, we use subtraction operator (-).

Example:

Lets subtract 50 from 100.

<?php

$num1=100;

$num2=50;

echo $num1-$num2; ?>

Copy the code into index file, save it and run it.

It would print 50. Similar operation we can perform on decimal numbers too. Try yourself and observe the result.

Multiplication Operation using PHP:

To perform multiplication operation in PHP, we use multiplication operator (*)

Example:

Lets multiply 50 with 100.

<?php 

$num1=50;

$num2=100;

echo $num1*$num2; ?>

Copy above code into index file, save it and run it. You would get 5000 on the webpage.

Division Operation using PHP:

To perform division operation in PHP, we use division operator (/).

Example:

Lets divide 500 by 100.

<?php

$num1=500;

$num2=100;

echo $num1/$num2; ?>

Copy the above code into index file, save it and run it. It would print 5 on the webpage.

Example:

Lets divide 100 by 3.

<?php

$num1=100;

$num2=3;

echo $num1/$num2; ?>

Copy the above code into index file, save it and run it. It would print 33.333333333333 on the webpage.

Note: As I have already mentioned, we don’t have to bother about the result datatype, because PHP engine can understand for the result value datatype and print accordingly. (In other programming language, Java, c, c++ we have to understand the type of result an arithmetic operation is going to give and need to capture in similar datatype but in PHP we are free from such pre-understanding. PHP engine is going to take care of it.)

Modulus Operation using PHP:

To perform modulus operation in PHP, we use modulus operator (%)

What is modulus operation?

In certain cases we may want to know the remainder value of a division operation. For example, if we divide 12 by 10 then we are going to get 2 as remainder. But if we simply perform division then it would give us 10.2 result.

<?php 

$num1=12;

$num2=10;

echo $num1/$num2; ?>

But our requirement in this situation is different. We are intending to collect the remainder of this division operation. We can perform certain task and get the remainder but luckily PHP has made this job easy. By using modulus operator we can collect remainder value directly.

<?php

$num1=12;

$num2=10;

echo $num1%$num2; ?>

Copy the above code in index file, save it and run it. You would see 2 on the webpage.

Using respective operator, we can perform various arithmetic operation in PHP.