PHP Operators

Operators in PHP are symbols that are used to execute operations on operands. In simple words, php operators are used to perform different operations on variables and their values.

For example: In 4+5, + is an operator and 4 and 5 are values.

Types of PHP Operators:

In php operators are divided into following types:

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical (or Relational) Operators
  • Increment/Decrement operators
  • Conditional (or ternary) Operators
  • String Operators
  • Array Operators

Now we will discuss these operators one by one.

PHP Arithmetic Operators

The PHP arithmetic operators are used to perform basic arithmetic operations with numeric values, such as addition, subtraction, Multiplication and so on.

Lets suppose we have two operands x=10 , y=5 now we will perform different arithmetic operations on them below:

OperatorNameDescriptionExample
+AdditionAdd the operands.$x+$y=15
Subtractionsubtract second value from first.$x-$y=5
*MultiplicationMultiply operands.$x*$y=50
/DivisionQuotient of operands$x/$y=2
%ModulusRemainder of operands$x%$y=0
**Exponentiationfirst operand raise to power of second operand$x ** $y=100000

PHP Assignment Operators

Assignment operators are used to give value to a variable. “=” is the most commonly used assignment operator.

OperatorNameDescriptionExample
=AssignAssigns values from right side operands to left side operands with this simple assignment operator.Z = X+Y 
 Assign  value of X + Y to Z
+=Add then assignIt adds the right and left operands together and assigns the result to the left operand.X+= Y is equivalent to X = X + Y
-=Subtract then assignIt takes the right operand and subtracts it from the left operand, then assigns the result to the left operand.X-= Y is equivalent to X = X – Y
*=Multiply then assignIt multiply the right and left operands together and assigns the result to the left operand.X*= Y is equivalent to X = X * Y
/=Divide then Assign
(quotient)
It divides the left operand from right operand and assigns the result to the left operand.X/= Y is equivalent to X = X / Y
%=Divide then Assign
(remainder)
It includes two operands to calculate modulus and assigns the result to the left operand.X%= Y is equivalent to X = X % Y

PHP Comparison Operators:

Comparison operators are used to compare two values(such as number or string) and show the final result in Boolean form.

Lets suppose we have two operands x=10 , y=5 now we will perform different arithmetic operations on them below:

OperatorNameDescriptionExample
==EqualChecks if the values of two operands are equal or not; if they are equal than it returns true.(x == y) is not true.
===IdenticalChecks if the values of two operands are equal and also the data type is same, than it returns true.(x === y) is not true.
!=Not equalChecks if the values of two operands are equal or not; if they are not equal than it returns true.(x != y) is true.
<>Not equalChecks if the values of two operands are equal or not; if they are not equal than it returns true.(x <> y) is true.
!==Non identicalChecks if the values of two operands are unequal or the data type are different, than it returns true.(x !== y) is true.
>Greater thanChecks if the left operand’s value is greater than the right operand’s value, than it returns true.(x > y) is true.
<Less thanChecks if the left operand’s value is smaller than the right operand’s value, than it returns true.(x < y) is not true.
>=Greater than or equal toChecks if the left operand’s value is greater than or equal to the right operand’s value, than it returns true.(x >= y) is true.
<=less than or equal toChecks if the left operand’s value is smaller than or equal to the right operand’s value, than it returns true.(x <= y) is not true.

PHP Logical Operators:

They are primarily used to manipulate conditional statements and expressions. Conditional statements are those that are based on a set of conditions. Furthermore, since a condition can be fulfilled or not, the outcome of a conditional statement can be true or false.

OperatorNameDescription
andLogical ANDIf both operands are true, the result is true.
orLogical ORIf any one of the operands is true, the result is true.
xorLogical XORTrue if either of the operand is true, false if both operands are true.
&&Logical ANDIf both operands are true, the result is true.
||Logical ORIf any one of the operands is true, the result is true.
!Logical NOTNot operator is used to reverse the logical state of operand. if operand is true it makes it false and vice versa.

PHP Increment / Decrement Operators:

To increase or decrease the value of a variable, increment and decrement operators are used. These are also known as unary operators because they only operate with single operands.

OperatorNameDescription
++xPre-IncrementFirstly $x is incremented by one , then returned.
–xPre-DecrementFirstly $x is decremented by one , then returned.
x++Post-IncrementReturn $a, then increase $a’s value by one.
x–Post-DecrementReturn $a, then decrease $a’s value by one.

PHP String Operators:

String operators are used to manipulate strings.

OperatorNameDescription
.ConcatenationConcatenate both Strings
.=Concatenation assignmentFirst concatenate strings, then assign the concatenated string to left string, e.g. $x = $x . $y

PHP Conditional Assignment Operators:

These operators compare two values and set a value  depending on conditions. The order of execution for this operator is from left to right

These are also used as a shorthand notation for the if…else statements that we’ll see in the decision-making article.

Syntax:
OperatorNameSyntaxDescription
?:Ternary(Condition) ? (Statement1) : (Statement2);If condition is true ? then statement1 : or else statement2 . This means that if condition is true then left result of the colon is implemented otherwise right statement is executed.
??Null Coalescing (Condition)?(Statement1)?(Statement2);It checks whether first variable is NULL or not and returns the first operand if the variable exists and is not NULL, otherwise the second operand is returned.

PHP Array Operators:

In the case of arrays, the array operators are used. These operators are primarily used to compare the values of arrays.

OperatorNameDescriptionExample
+UnionUnion of $x and $y$x + $y
==EqualIf both arrays have the same key-value pair, then it returns true.$x == $y
===Identical If both arrays have same key-value pair in the same order and of same type, then it returns true.$x === $y
!=Not equalIf both arrays are unequal, then it returns true.$x != $y
<>Not equalIf both arrays are unequal, then it returns true.$x <>$ y
!==Non identicalIf both arrays are not identical to each other, then it returns true.(x !== y) is true.