PHP - Operators

  »   PHP Introduction  »   PHP Tutorial - Operators

You can find logical, arithmetic, or comparison operators all around you. If you learned any other programming language or paid attention to a few mathematics classes, you probably all ready are familiar with them. We use logical operators in PHP to execute operations on variables and/or values.

Operators are mainly divided using the following criteria. Some of them have a few special cases that we will explain on the way.

  • Arithmetical operations
  • Assignment operators
  • Comparison operators
  • Logical operators

PHP arithmetic operators

The PHP arithmetic operators are mostly self-explaining. They normally are used to perform mathematical operations between numbers, such as addition, subtraction, multiplication etc.

OperatorNameUsageExplenation
+Addition$x + $ySum of $y added to $x
-Subtraction$x - $yDifference of $y subtracted from $x
*Multiplication$x * $yProduct of $x multiplied by $y
/Division$x / $yQuotient of $x divided by $y
%Modulo$x % $yRemainder of $a divided by $b.
**Exponentiation$x ** $yElevation of $x to the $y'th power.

PHP assignment operators

The PHP assignment operators are used to write a value into a variable. You may already know the "=" assignment operator. This is the simplest one and it means that the value from the right is assigned to the variable on the left.

, On the other hand, we will include here some operation that combines assignment and the above arithmetical operation. This is just shorthand form of writing arithmetical operation and assigning the value to a variable.

OperatorNameExplanationUsageEquivalent
=EqualBasic value assignation$x = $y
+=Plus EqualAddition operation$x += y$x = $x + $y
-=Minus EqualSubtraction operation$x -= $y$x = $x - $y
*=Multiply EqualMultiplication operation$x *= $y$x = $x * $y
/=Divide EqualDivision operation$x /= $y$x = $x / $y
%=Modulo EqualModulus operation$x %= $y$x = $x % $y
.=Concatenate EqualConcatenate operation$x .= $y$x = $x . $y

Increment and decrement operators

There are another assignment operators that are used to increase or decrees the value of a variable by 1

The $i++ and $i-- respectively are equivalent (or shorthand) operations of the following:

php$i = $j = 1;
$i++;     // is the same as $i += 1; and  $i = $i + 1;
echo $2;  // prints 1

$j--;     // is the same as $j -= 1; and  $j = $j - 1;
echo $j;  // prints 0

There is also another way to perform the same operation

php$i = $j = 1;
++$i;
echo $i;  // prints 2

--$j;
echo $j;  // prints 0

Differences between post and pre increment/decrement operations

Although many times, this operation result is the same, there are few minor logical differences. Try directly printing the $i++ value. You will be surprised.

php$a = $b = $c = $d = 5;

echo $a++; // prints 5
echo ++$b; // prints 6
echo $c--; // prints 5
echo --$d; // prints 4

I am sure you already notice the difference and know the logic behind it

  • $i++ =>return value and then increase by 1
  • ++$i =>increase by 1 and then return value
  • $i-- =>return value and then decrease by 1
  • --$i =>decrease by 1 and then return value

PHP comparison operators

Comparison operators are mostly used in if statements and conditional expressions in general. Comparison operations always evaluate to either true or false.

Assume: $x = 2 and $y = 3;
OperatorNameUsageEvaluate to...
==Equal$x == $yReturns true if $x is equal to $y
===Identical$x === $yReturns true if $x is equal to $y, and they are of the same type
!=Not equal$x != $yReturns true if $x is not equal to $y
<>Not equal$x <> $yReturns true if $x is not equal to $y
!==Not identical$x !== $yReturns true if $x is not equal to $y, or they are not of the same type
>Greater than$x > $yReturns true if $x is greater than $y
<Less than$x < $yReturns true if $x is less than $y
>=Greater than or equal to$x >= $yReturns true if $x is greater than or equal to $y
<=Less than or equal to$x <= $yReturns true if $x is less than or equal to $y

PHP logical operators

As you already guessed, comparison operators, allow you to compare two or more values of various conditional statements

OperatorNameUsageResult
andAnd$x and $yTrue if both $x and $y are true
orOr$x or $yTrue if either $x or $y is true
xorXor$x xor $yTrue if either $x or $y is true, but not both
&&And$x && $yTrue if both $x and $y are true
||Or$x || $yTrue if either $x or $y is true
!Not!$xTrue if $x is not true

logical operator cheat sheet table

The && and || logical operators seem to have more general recognition and background usage, although the and and or form of the same logical operators is taking more and more acceptation for the sake of readability.

Logical operator AND ( && )

phpfalse and false => false
false and true => false
true and false => false
true and true => true

Logical operator OR ( || )

phpfalse or false => false
false or true => true
true or false => true
true or true => true

Logical operator NOT ( ! )

php!false => true
!true => false

PHP logical operators applied to arrays

You may find this normally as array operators, but they are same good all logical operators applied to arrays. Arrays are basically a type of variables that can store multiple values.

OperatorNameUsageResult
+Union$x + $yUnion of $x and $y
==Equality$x == $yReturns true if $x and $y have the same key/value pairs
===Identity$x === $yReturns true if $x and $y have the same key/value pairs in the same order and of the same types
!=Inequality$x != $yReturns true if $x is not equal to $y
<>Inequality$x <> $yReturns true if $x is not equal to $y
!==Non-identity$x !== $yReturns true if $x is not identical to $y

The instanceof logical operator

Instanceof is used to determine whether a PHP variable is an instantiated object of a certain class. instanceof can also be used to determine whether a variable is an instantiated object of a class that inherits from a parent class, but this is not the subject of this tutorial. You can read more about this on the php.net manual site.