PHP Operators

PHP

In this tutorial we will learn about the different types of operators used in PHP.

  • Arithmetic Operators
  • Assignment Operators
  • Bitwise Operators
  • Comparison Operators
  • Logical Operators
  • Increment/Decrement Operators

Arithmetic Operators

These operators are addition, subtraction, multiplication, division and modulus.

OperatorExample
+ (addition)1 + 2 = 3
- (subtraction)2 - 1 = 1
* (multiplication)4 * 5 = 20
/ (division)4 / 2 = 2
% (modulus)5 % 2 = 1 (we get the remainder)

Assignment Operators

To assign value to any variable we use the assignment operator.

$name = "Yusuf Shakeel";	//we have assigned a string value to a variable.

We can also combine assignment operator with other operators like +, - etc. to shorten certain expression.

In the following expression we are assigning 10 to variable x and 20 to variable y. We are then adding x and y and saving the final result in variable x.

$x = 10;
$y = 20;
$x = $x + $y;

The above addition expression can be simplified in the following manner.

$x = 10;
$y = 20;
$x += $y;	//this means $x = $x + $y

Following are combined assignment operators.

Combined Assignment OperatorExpanded form
$x += $y$x = $x + $y
$x -= $y$x = $x - $y
$x *= $y$x = $x * $y
$x /= $y$x = $x / $y
$x %= $y$x = $x % $y

Bitwise Operators

Bitwise operators are used to work with individual bits of integer values. Following are the list of bitwise operators.

OperatorExplanationExample
& (and)If both the bits are 1 then output is 11010 & 1100 = 1000
| (or)If any one of the bits is 1 then output is 11010 | 1100 = 1110
~ (not)Changes 1 to 0 and 0 to 1~1011 = 0100
^ (xor)If only one of the bit is set to 1 and not both then output is 11010 ^ 1100 = 0110
<< (left shift)Shift the bits by N position to the left000011 << 2 = 001100
>> (right shift)Shift the bits by N position to the right001100 >> 2 = 000011

Comparison Operators

Comparison operators are used to compare the two operands and the result is in boolean form i.e, either true or false.

OperatorExplanationExample
== (equal)If both sides are equal then return true$x == $y
true if $x is equal to $y
=== (identical)If both sides are equal and of the same type then return true$x === $y
true if $x is equal to $y and both are of same type
!= (not equal)If one is not equal to the other then return true$x != $y
true if $x is not equal to $y
!== (not identical)If one is not equal to the other and also not of the same type then return true$x !== $y
true if $x is not equal to $y and they are also not of the same type
< (less than)If left side is less than the right side then return true$x < $y
true if $x is less than $y
> (greater than)If left side is greater than the right side then return true$x > $y
true if $x is greater than $y
<= (less than or equal to)If left side is less than or equal to the right side then return true$x <= $y
true if $x is less than or equal to $y
>= (greater than or equal to)If left side is greater than or equal to the right side then return true$x >= $y
true if $x is less than or equal to $y

Logical Operators

Logical operators are used to work with Boolean values i.e., true or false. Let us first see the boolean values assigned by PHP to some expressions.

Following have boolean value false.

  • Literal Value false
  • Empty string " "
  • Zero string "0"
  • Integer value 0
  • Floating value 0.0
  • Array with zero elements
  • Null value

All other values evaluate to true.

OperatorExampleExplanation
&&$x && $ytrue if both $x and $y evaluate to true, otherwise result is false
||$x || $ytrue if either $x or $y evaluate to true, otherwise result is false
!!$xtrue if $x evaluate to false and vice versa
and$x and $ytrue if both $x and $y evaluate to true, otherwise result is false
or$x or $ytrue if either $x or $y evaluate to true, otherwise result is false
xor$x xor $ytrue if $x or $y and not both evaluate to true, otherwise result is false

Increment/Decrement Operators

Increment operator is used to increase value of a variable by 1. And decrement operator is used to decrease the value by 1.

++ is the Increment Operator and -- is the Decrement Operator.

$x = 10;		//x set to 10

echo $x;	//output 10

//now increase the value of x by 1
$x = $x + 1;

echo $x 	//output 11

Now using the increment operator.

$x = 10;		//x set to 10

echo $x;	//output 10

//now increase the value of x by 1
$x++;

echo $x 	//output 11

Decrement operation

$x = 10;		//x set to 10

echo $x;	//output 10

//now decrease the value of x by 1
$x = $x - 1;

echo $x 	//output 9

Now using the decrement operator.

$x = 10;		//x set to 10

echo $x;	//output 10

//now decrease the value of x by 1
$x--;

echo $x 	//output 9

Important points to note when using the increment and decrement operators in expression.

++$x; // adds one to $x and then returns the result
$x++; // returns value of $x and then adds one to $x
–-$x; // subtracts one from $x and then returns the result
$x–-; // returns value of $x and then subtracts one from $x

Lets check some examples.

Pre-increment (++ before the variable)

//set x to 10
$x = 10;

echo $x;	//output 10

//now assign value of x to y using pre-increment
//this is first increment x by 1 and then assign the new value to y
$y = ++$x;

echo $y;	//output 11

echo $x;	//output 11

Post-increment (++ after the variable)

//set x to 10
$x = 10;

echo $x;	//output 10

//now assign value of x to y using post-increment
//first use value of x then increment by 1
$y = $x++;

echo $y;	//output 10

echo $x;	//output 11

Pre-decrement (-- before the variable)

//set x to 10
$x = 10;

echo $x;	//output 10

//now assign value of x to y using pre-decrement
//this will first decrease value of x by 1 then assign to y
$y = --$x;

echo $y;	//output 9

echo $x;	//output 9

Post-decrement (-- after the variable)

//set x to 10
$x = 10;

echo $x;	//output 10

//now assign value of x to y using post-decrement
//this will first use the value of x then decrease by 1
$y = $x--;

echo $y;	//output 10

echo $x;	//output 9