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 `$c=$a+$b`

, `'+'`

is an addition operator which will add the values of `'$a'`

and `'$b'`

and `'='`

is an assignment operator which will assign the sum to `'$c'`

.

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
- Logical or Relational Operators
- Assignment Operators
- Comparison Operators
- Conditional or Ternary Operators
- PHP Increment/Decrement Operators
- String Operators
- Array Operators
- Spaceship Operators (
**Introduced in PHP 7**)

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:

Operator | Name | Example | Result |
---|---|---|---|

+ | 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 `'if'`

statements, `'for'`

loops, `'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:

Operator | Name | Example | Description |
---|---|---|---|

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.

Assignment | Same as... | Description |
---|---|---|

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`

.
Comparison Operators `check for equality, identical values, size comparability`

, etc.
Look at the table below to see all the comparison operators and there use:

Operator | Name | Example | Result |
---|---|---|---|

== | 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-else'`

statement.
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`

.

Operator | Name | Description |
---|---|---|

?: | Ternary | `(Condition) ? (expr1) : (expr2);` . This means that ifthe given condition is true then the left expression of the colon is accepted otherwise the right one. |

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:

Operator | Name | Description |
---|---|---|

$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.

Operator | Name | Example | Description |
---|---|---|---|

. | 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:

Operator | Name | Example | Description |
---|---|---|---|

+ | 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:

Condition | Syntax | Return Value |
---|---|---|

$a < $b | $a <=> $b | -1 ($b is greater) |

$a > $b | $a <=> $b | 1 ($a is greater) |

$a == $b | $a <=> $b | 0 (both are equal) |

List of color names supported by all browser »

List of character codes to display special character »

List of all language supported by all browser »

List of Country code supported by all browser »