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:
Operator | Name | Description | Example |
+ | Addition | Add the operands. | $x+$y=15 |
– | Subtraction | subtract second value from first. | $x-$y=5 |
* | Multiplication | Multiply operands. | $x*$y=50 |
/ | Division | Quotient of operands | $x/$y=2 |
% | Modulus | Remainder of operands | $x%$y=0 |
** | Exponentiation | first 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.
Operator | Name | Description | Example |
= | Assign | Assigns 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 assign | It 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 assign | It 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 assign | It 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:
Operator | Name | Description | Example |
== | Equal | Checks if the values of two operands are equal or not; if they are equal than it returns true. | (x == y) is not true. |
=== | Identical | Checks 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 equal | Checks if the values of two operands are equal or not; if they are not equal than it returns true. | (x != y) is true. |
<> | Not equal | Checks if the values of two operands are equal or not; if they are not equal than it returns true. | (x <> y) is true. |
!== | Non identical | Checks if the values of two operands are unequal or the data type are different, than it returns true. | (x !== y) is true. |
> | Greater than | Checks if the left operand’s value is greater than the right operand’s value, than it returns true. | (x > y) is true. |
< | Less than | Checks 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 to | Checks 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 to | Checks 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.
Operator | Name | Description |
and | Logical AND | If both operands are true, the result is true. |
or | Logical OR | If any one of the operands is true, the result is true. |
xor | Logical XOR | True if either of the operand is true, false if both operands are true. |
&& | Logical AND | If both operands are true, the result is true. |
|| | Logical OR | If any one of the operands is true, the result is true. |
! | Logical NOT | Not 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.
Operator | Name | Description |
++x | Pre-Increment | Firstly $x is incremented by one , then returned. |
–x | Pre-Decrement | Firstly $x is decremented by one , then returned. |
x++ | Post-Increment | Return $a, then increase $a’s value by one. |
x– | Post-Decrement | Return $a, then decrease $a’s value by one. |
PHP String Operators:
String operators are used to manipulate strings.
Operator | Name | Description |
. | Concatenation | Concatenate both Strings |
.= | Concatenation assignment | First 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:
Operator | Name | Syntax | Description |
?: | 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.
Operator | Name | Description | Example |
+ | Union | Union of $x and $y | $x + $y |
== | Equal | If 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 equal | If both arrays are unequal, then it returns true. | $x != $y |
<> | Not equal | If both arrays are unequal, then it returns true. | $x <>$ y |
!== | Non identical | If both arrays are not identical to each other, then it returns true. | (x !== y) is true. |