We use comparison operators for comparing two values such as number or string.
Operator
Name
Syntax
Explanation
Equal (==)
Equality
$a == $b
Returns TRUE if $a is equal to $b.
Identical (===)
Identical
$a === $b
Returns TRUE if $a is equal to $b and both have the same data type.
Not Equal (!=)
Inequality
$a != $b
Returns TRUE if $a is not equal to $b.
Not Equal (<>)
Inequality
$a <> $b
Returns TRUE if $a is not equal to $b.
Not Identical (!==)
Not Identical
$a !== $b
Returns TRUE if $a is not equal to $b or their data types are different.
Less Than (<)
Less Than
$a < $b
Returns TRUE if $a is less than $b.
Greater Than (>)
Greater Than
$a > $b
Returns TRUE if $a is greater than $b.
Less Than or Equal (<=)
Less Than or Equal
$a <= $b
Returns TRUE if $a is less than or equal to $b.
Greater Than or Equal (>=)
Greater Than or Equal
$a >= $b
Returns TRUE if $a is greater than or equal to $b.
Spaceship (<=>)
Spaceship
$a <=> $b
Returns -1 if $a < $b, 0 if $a = $b, 1 if $a > $b.
Incrementing/Decrementing Operators
We use these operators for incrementing and decrementing the value of a variable.
Operator
Name
Syntax
Explanation
Post Increment (++)
Increment
$a++
Increments $a by 1, then returns $a.
Pre Increment (++)
Increment
++$a
Increments $a by 1, then returns $a.
Post Decrement (--)
Decrement
$a--
Decrements $a by 1, then returns $a.
Pre Decrement (--)
Decrement
--$a
Decrements $a by 1, then returns $a.
Logical Operators
We use logical operators to perform bit-level operations on operands.
Operator
Name
Syntax
Explanation
AND (and, &&)
Logical AND
$a and $b, $a && $b
Returns TRUE if both $a and $b are TRUE.
**OR (or,
)**
Logical OR
XOR (xor)
Logical XOR
$a xor $b
Returns TRUE if $a or $b is TRUE, but not both.
NOT (!)
Logical NOT
!$a
Returns TRUE if $a is not TRUE.
String Operators
We use string operators to perform operations on strings.
Operator
Name
Syntax
Explanation
Concatenation (.)
Concatenate
$a . $b
Concatenates $a and $b.
Concatenation and Assignment (.=)
Concatenate and Assign
$a .= $b
Concatenates $b to $a and assigns it to $a.
Array Operators
We use array operators to compare the values of arrays.
Operator
Name
Syntax
Explanation
Union (+)
Union
$a + $b
Combines arrays $a and $b.
Equality (==)
Equality
$a == $b
Returns TRUE if arrays $a and $b have same key-value pairs.
Inequality (!=)
Inequality
$a != $b
Returns TRUE if arrays $a and $b are not equal.
Identity (===)
Identity
$a === $b
Returns TRUE if arrays $a and $b have same key-value pairs in the same order.
Non-Identity (!==)
Non-Identity
$a !== $b
Returns TRUE if arrays $a and $b are not identical.
Execution Operators
PHP uses backticks to run shell commands.
Operator
Name
Syntax
Explanation
Backticks (``)
Execute Shell Command
`$a`
Executes the shell command and returns the result.
Error Control Operators
When we use the error control operator with an expression, the error message gets ignored by that expression.
Operator
Name
Syntax
Explanation
At (@)
Error Control
@expression
Suppresses error messages from an expression.
Conditional Assignment Operators
Conditional Assignment Operators are used to setting values based on the following situations.
Operator
Name
Syntax
Explanation
Ternary (?:)
Ternary
$a = expr1 ? expr2 : expr3
Returns expr2 if expr1 is TRUE; otherwise, returns expr3.
Null Coalescing (??)
Null Coalescing
$a = expr1 ?? expr2
Returns expr1 if it is not NULL, otherwise returns expr2.
Frequently Asked Questions
What is '@' in PHP?
The @ operator in PHP suppresses error messages generated by an expression, preventing them from being displayed.
Why do we use :: in PHP?
The :: operator in PHP is used to access static methods, constants, or properties within a class.
What is the operator and type?
An operator in PHP is a symbol that performs operations on variables and values, such as arithmetic, logical, and comparison operations.
Conclusion
This article teaches about PHP Operators and how we use them. We saw why it could be beneficial for a developer to learn. Click here to read about PHP Interview Questions.