PHP Operators or simply operators are used to perform different kind of operations on some values which
can modify their value or state or can be used to compare with other values.
So, we can say that Operators perofrm some operation on a some values and give us a result which we can use somewhere.
For example, in
'+' is an addition operator which will add the values of
'=' is an assignment operator which will assign the sum to
In this tutorial, we will go through different kind of Operators which are used in PHP. We will explain each type of operators one by one. Don't take this lightly, operators are used everywhere in a Script/Program, you should know exactly what they do these and how to use them. Given below are the various groups of operators:
Arithmetic operators are use to perform simple mathematical operations like
multiplication, addition, subtraction,etc.
Look at the table below to see all the arithmetic operators with their examples:
|+||Addition||$a + $b||Sum of $a and $b|
|-||Subtraction||$a - $b||Difference of $a and $b|
|*||Multiplication||$a * $b||Product of $a and $b|
|/||Division||$a + $b||Quotient of $a and $b|
|%||Modulus||$a % $b||Remainder of $a divided by $b|
|**||Exponentiation||$a ** $b||Result of $a raise to the power $b|
The Logical Operators are used with conditional statements which
test a condition to be true or false.
These operators are usually used with
'while' loops, etc.
In all these statements and loops the common step is to test a condition and then a decision is taken, according to the result of the test.
Look at the table below for all the logical operators and their example:
|and||And||$a and $b||Returns True if both $a and $y are true|
|or||Or||$a or $b||Returns True if either $a or $b is true|
|xor||Xor||$a xor $b||Returns True if either $a or $b is true, but not both|
|&&||And||$a && $b||Alternate for 'and' operator|
|||||Or||$a || $b||Alternate for 'or' operator|
|!||Not||!$a||Returns True if $a is not true|
Assignment Operator is
used to assign a value to a variable. You can directly assign a value to a variable or you can also store the result of any expression to any variable using assignment operator. The basic assignment operator is
'=', which can be used in different ways to assign values to a variable. Look at the table below to see all the possible ways to use this operator.
|x += y||x = x + y||Addition|
|x -= y||x = x - y||Subtraction|
|x *= y||x = x * y||Multiplication|
|x /= y||x = x / y||Division|
|x %= y||x = x % y||Modulus|
Comparison Operators are
used to compare two values.
check for equality, identical values, size comparability, etc.
Look at the table below to see all the comparison operators and there use:
|==||Equal||$a == $b||Returns true if $a is equal to $b|
|===||Identical||$a === $b||Returns true only if $a is equal to $b, and they are also of the same datatype|
|!=||Not equal||$a != $b||Returns true if $a is not equal to $b|
|<>||Not Equal||$a <> $b||Returns true if $a is not equal to $b|
|!==||Not identical||$a !== $b||Returns true if $a is not equal to $b or they are not of the same datatype|
|>||Greater than||$a > $b||Returns true if $a is greater than $b|
|<< /td>||Less than||$a < $b||Returns true if $a is less than $b|
|>=||Greater than or equal to||$a >= $b||Returns true if $a is greater than or equal to $b|
|<=< /td>||Less than or equal to||$a <= $b||Returns true if $a is less than or equal to $b|
These operators are an alternative for
If your block of 'if-else' statement is very short then it is better to use these operators than writing full 'if-else' block, but you are free to use any approach.
With these operators, we first check an expression and then take decisions based on whether the outcome is
TRUE or FALSE.
These operators are
used to increment or decrement values, these are also called unary operators because they work on a single operand.
Look at the table below to see all the possible ways to use these operators:
|$a = ++$b||Pre-increment||Increments $b's value by one, and then assign it to $a|
|$a = $b++||Post-increment||Assign $b's value to $a first and then increment the value of $b by one|
|$a = --$b||Pre-decrement||Decrements $b's value by one, then assign it to $a|
|$a = $b--||Post-decrement||Assign $b's value to $a first and then decrement the value of $b by one.|
These operators are
used with strings and are used to manipulate them.
These operators are used to concatenate two or more strings together.
|.||Concatenation||$txt1 . $txt2||Concatenation of $txt1 and $txt2|
These are operators which we have seen above but this time we will use some of them with arrays and tha's why these are called array operators. Look at the table below which shows the operation on two arrays $a and $b:
|+||Union||$a + $b||Union(Merging) of $a and $b|
|==||Equality||$a == $b||Returns true if $a and $b have the same key/value pairs|
|===||Identical||$a === $b||Returns true if $a and $b have the same key/value pairs in the same order and of the same datatype|
|!=||Inequality||$a != $b||Returns true if $a is not equal to $b|
|<>||Inequality||$a <> $b||Returns true if $a is not equal to $b|
|!==||Non-identity||$a !== $b||Returns true if $a is not identical to $b|
Spaceship Operator(<=>) is
used to compare two values as whether the first value is greater than, equal or less than the 2nd value.
It returns different integer values like,
'-1' if left operand is smaller than the right operand,
'0' if both are equal, and '1' if left operand is greater than right operand.
If the left operand is greater, it returns 1. The following table shows how it works in detail:
|$a < $b||$a <=> $b||-1 ($b is greater)|
|$a > $b||$a <=> $b||1 ($a is greater)|
|$a == $b||$a <=> $b||0 (both are equal)|