Operators In PHP

This article is the second of a series of PHP guides that aim at teaching you the basics of PHP programming. 

Dear pupils,

I hope you would have been practising the last lecture ( PHP Programming Basics ) as I am getting feedback from different students from different countries and I am very happy I am able to contribute to PHP through my series of PHP Articles for beginners and professionals.

Today we are going to discuss different types of operators used in PHP. I hope you remember the basic definition of the operators and operands from my last article ( PHP Programming Basics ) and I am not going to explain it again as we had promised in the very first article of this Series of PHP Tutorials that we will not see behind and this is just to compell you to concentrate on each and every article of this series and practise (O! I love practice)

PHP offers the following operators to perform different operations on the operands.

Arithmetic Operators:

Arithmetic operators enable you to perform different mathematical operations on different values.

Below are the quick details of arithmetic operators,

Addition: it is denoted by plus sign ( + ) and is used to add two values e.g 2 + 3 = 5

you can add variables as following,

// addition
$var1 = 4;
$var2 = 5;
$total = $var1 + var2;
echo "Total = " . $total;
// Total = 9

Subtraction: it is denoted by minus sign ( - ) and is used to subtract first value from the second e.g 2 - 1 = 1

you can subtract two variables as following,

// subtraction
$var1 = 8;
$var2 = 5;
$total = $var1 - var2;
echo "Total = " . $total;
// Total = 3

multipication: it is denoted by Asterisk sign ( * ) and is used to multiply two value e.g 2 * 3 = 6

you can multiply two variables as following,

// Multipication
$var1 = 8;
$var2 = 5;
$total = $var1 * var2;
echo "Total = " . $total;
// Total = 40

Division: it is denoted by forward slash sign ( / ) and is used to divide first value on the second e.g 2 / 2 = 1

you can divide two variables as following,

// Division
$var1 = 15;
$var2 = 5;
$total = $var1 /  var2;
echo "Total = " . $total;
// Total = 3

Modulus(Division Remainder): it is denoted by percentage sign ( % ) and is used to give the division remainder e.g 3 % 2 = 1

you can find the modulus of two variables as following,

// Modulus
$var1 = 15;
$var2 = 5;
$total = $var1 % var2;
echo "Total = " . $total;
// Total = 0

Increment: it is denoted by double-plus sign ( ++ ) and is used to increase value of one variable by one e.g

// Increment
$counter = 15;
$counter ++;   // 16
$counter ++;  // 17
echo "Counter = " . $counter;
// Counter = 17

Remember there are two types of incrementation,

a) Prefix Increment: in which the increment operator comes before the variable name i.e ++$var;

In this type of increment the variable’s value is incremented and is available for the immediate use e.g

// Prefix Increment
$counter = 15;
echo "Counter = " . ++$counter;
// Counter = 16

b) Postfix Increment: in which increment operation is performed however the resultant value is not available for the

immediate use and can be used only when the control access the variable in the next run e.g

// Postfix Increment
$counter = 15;
echo "Counter = " . $counter++;
// Counter = 15

Decrement Operator: it is denoted by double-minus sign ( — ).

It is very similar to the increment operator however it decrease the value by one.

Assignment Operators:

Assignment operators are used to assign new values to a variable. We have the following assignment operator available in PHP,

$var1 = 3;
$var2 = 5;
$var1 = $var2;    //var1=5
$var1 += $var2;  //var1=8
$var1 -= $var2;  //var1= -2
$var1 *= $var2; //var1=15
$var1 /= $var2; //var1=0.6
$var1 %= $var2; //var1=3

//you can print $var1 after each step to see its value

First of all in above all example i am assuming each operation for the basic values of var1 and var2 to be 3 and 5 respectively.

Comparison Operators:

comparison operators are used to compare two values. We can use following comparison operators in PHP,

( == ) is equal to
( != ) not equal to
( > ) greater than
( < ) less than
( >= ) greater than or equal to
( <= ) less than or equal to

Just learn these operators by heart we will use them in our up coming articles and you will be able to understand their exact use.

Logical Operators:

We have three logical operators in PHP,

AND ( && ) Operators: you can use both ‘AND’ and double-empersand sign ( && ) for this operator in PHP. The basic logical use of this operator in a rough psuedocode could be

If first condition is true && second condition is also true

OR ( || ) Operators: you can use both ‘OR’ and double-pipeKey sign ( || ) for this operator in PHP. The basic logical use of this operator in a rough psuedocode could be

Either the first condition is true || the second condition is true

NOT ( ! ) Operators: you can use both ‘NOT’ and Exclamination Mark (Mathematician will call it Factorial Sign) sign ( ! ) for this operator in PHP. The basic logical use of this operator in a rough psuedocode could be

First value is NOT equal to the second value

Again do not confuse the use of these operators but just learn them by heart and I will use these all operators in my up coming articles for more complex PHP programmes and then your all doubts will be removed hopefully.

This is one of the most important articles of this PHP Tutorials Series. Please concentrate hard on this articles and try to understand all issue discussed today and feel free to ask if you have any question and remember if you cannot understand this foundation lectures of PHP it will be very hard for you to survive when we will ‘BURN IT’ as I had promised with you that I will take you the Extreme PHP Programming and the Only rule of our game is “Don’t look behind” so you will not be able to go back to these articles again so learn these things now this article will make your strong foundation towards complex programming.

Let’s end this lecture here.

Haroon Ahmad

Share this page:

Suggested articles

5 Comment(s)

Add comment



Don't forget the (===) operator for mathing type as well as string value. I've found this operator ESSENTIAL to properly evaluating some function built into PHP.


yes it is very useful operator when you want to match the data types as well as values e.g


$value1 = 3;

$value2 = "3";

if($value1 === $value2)

echo "matched";


echo "not matched";


You will get the NOT MATCHED output because value1 has 3 as integer value on the other hand 3 is string in value2 variable. So whenever you need to match data type as well as value you should use === operator.


This tutorial is great. Just to let you know, the code examples you have listed for division and modulus are using the subtraction operator. Also in the assignment section, using the forward slash does give a float, and a backslash gives an error. Otherwise, nice work and good examples.


I have removed all typos. thanks for pointing them.

By: julio

Thanks for the quick tips.was looking at how to display in a table; to work like in excel.