Operators are used to perform operations on variables and values. When we’re talking about operators we’re basically talking about “math”. By operator I mean things like multiplication and division signs.
As you might imagine there are tons of operators. Certainly enough to get lost in. The reality is, a lot of these you won’t use on a regular basis or they’ll make more sense if we cover them in a different lesson.
PHP Operators can be separated into the following categories:
- PHP Arithmetic Operators
- PHP Assignment Operators
- PHP Incrementing / Decrementing Operators
- PHP Comparison Operators
- PHP Logical Operators
- Bitwise Operators
- PHP Array Operators
- PHP String Operators
An operator is something that takes one or more values (or expressions, in programming jargon) and yields another value (so that the construction itself becomes an expression).
The precedence of an operator specifies how “tightly” it binds two expressions together. For example, in the expression 1 + 5 * 3, the answer is 16 and not 18 because the multiplication (“*”) operator has a higher precedence than the addition (“+”) operator. Parentheses may be used to force precedence, if necessary. For instance: (1 + 5) * 3 evaluates to 18.
When operators have equal precedence their associativity decides how the operators are grouped. For example “-” is left-associative, so 1 – 2 – 3 is grouped as (1 – 2) – 3 and evaluates to -4. “=” on the other hand is right-associative, so $a = $b = $c is grouped as $a = ($b = $c).
Operators of equal precedence that are non-associative cannot be used next to each other, for example 1 < 2 > 1 is illegal in PHP. The expression 1 <= 1 == 1 on the other hand is legal, because the == operator has lesser precedence than the <= operator.
Although parentheses are not strictly needed, the readability of the code can often be increased by explicitly grouping rather than by using the implicit predominance and association of the operator.
|right||++ — ~ (int) (float) (string) (array) (object) (bool) @|
|left||* / %|
|left||+ – .|
|non-associative||< <= > >=|
|non-associative||== != === !== <> <=>|
|right||= += -= *= **= /= .= %= &= |= ^= <<= >>=|
The following table lists the operators in order of precedence, with the highest-precedence ones at the top. Operators on the same line have equal precedence, in which case associativity decides grouping.
PHP Arithmetic Operators
The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc.
|a + b||Addition||5 + 4||9|
|a – b||Subtraction||10 – 1||9|
|a * b||Multiplication||3 * 3||9|
|a / b||Division||18 / 2||9|
|a % b||Modulus||20 % 11|
8 % 2
|a . b||Concatenation||“Hello”.”World”||Hello World|
PHP Assignment Operators
The PHP assignment operators are used with numeric values to write a value to a variable.
The basic assignment operator in PHP is “=”. It means that the left operand gets set to the value of the assignment expression on the right.
|Assignment||Similar to …||Description|
|a = b||a = b||Value of expression on right is assigned to left operand. Here value of ‘b’ is assigned to operand ‘a’.|
|a += b||a = a + b||Addition|
|a -= b||a = a – b||Subtraction|
|a *= b||a = a * b||Multiplication|
|a /= b||a = a / b||Division|
|a %= b||a = a % b||Modulus|
|a .= b||a = a . b||Concatenation|
PHP Incrementing / Decrementing Operators
The PHP increment operators are used to increment a variable’s value.
The PHP decrement operators are used to decrement a variable’s value.
|++a||Pre-increment||Increments a by 1 and returns a|
|a++||Post-increment||Returns a, then increments by 1|
|–a||Pre-decrement||Decrements by 1 and returns a|
|a–||Post-decrement||Returns a, then decrements by 1|
PHP Comparison Operators
The PHP comparison operators are used to compare two values (number or string):
Comparison operators, as their name implies, allow you to compare two values. You may also be interested in viewing the type comparison tables, as they show examples of various type related comparisons.
|a == b||Equal||Returns true if a is equal to b||5 == 4 returns false|
|a === b||Identical||Returns true if a and b are equal and they are of same type||9 === “9” returns false|
|a != b|
a <> b
|Not equal||Returns true if a is not equal to b||3 != 2|
3 <> 2
|a !== b||Not Identical||Returns true if a is not equal to b or they are not of same type||9 !== “9” returns true|
|a > b||Greater than||Returns true if a is greater than b||9 > 3 returns true|
|a < b||Less than||Returns true if a is less than b||9 < 3 returns false|
|a >= b||Greater than or equal to||Returns true if a is greater than or equal to b||9 >= 3 returns true|
|a <= b||Less than or equal to||Returns true if a is less than or equal to b||9 <= 3 returns false|
PHP Logical Operators
The PHP logical operators are used to combine conditional statements.
|a and b|
a && b
|And||Returns true if both a and b are true||a = 3 b = 9|
(a < 5 and b > 4) returns true
(a < 5 && b > 10) returns false
|a or b|
a || b
|Identical||Returns true if either or both a and b are true||a = 9 b = 5|
(a > 3 or b == 8) returns true
(a == 5 || b > 9) returns false
|a xor b||Xor||Returns true if either a or b is true, but not both||a = 8 b = 5|
(a == 8 xor b == 5) returns false
|!a||Not||Returns true if a is not true||a = 9 b = 7|
!(a < b) returns true
Bitwise Operator In PHP
Another kind of operators are bitwise operators. They compare bits of variables with each other and allow evaluation and manipulation of specific bits within an integer.
The detailed illustrations of them are out of these lessons and just we introduce them and their names:
|$x & $y||AND||And together each bit contained in $x and $y|
|$x | $y||OR||Or together each bit contained $x and $y|
|$x ^ $y||XOR||Exclusive – or together each bit contained in $x and $y|
|~$x||NOT||Negate each bit in $x|
|$x << $y||Shift Left||$x will receive the value of $y shifted left tow bits|
|$x >> $y||Shift Right||$x will receive the value of $y shifted right two bits|
PHP Array Operators
The PHP array operators are used to compare arrays.
|a + b||Union||Union of a and b|
|a == b||Equality||Returns true if a and b have same key / value pairs|
|a === b||Identity||Returns true if a and b have same key / value pairs in the same order and of the same type|
|a != b|
a <> b
|Inequality||Returns true if a is not equal to b|
|a !== b||Non-identity||Returns true if a is not identical to b|
String operators in PHP :
PHP has two operators that are specially designed for strings.
Operator Name Example Result. Concatenation $txt1 . $txt2 Concatenation of $txt1 and $txt2.=Concatenation assignment$txt1 .= $txt2Appends $txt2 to $txt1
|$x = “vikram”. “y”||Concatenation||$x is assigned the string vikramy|
|$x .= “edmasters”;||Concatenation-assignment||$x equals its current value concatenated with “edmasters”|