In this tutorial, you shall learn about different categories of Operators in PHP, operators in each category, and how to use them in PHP programs, with dedicated tutorials to each operator.
PHP Operators
Operators perform operations on operands like variables or values.
In PHP, there are eight groups of operators based on the type of operations they perform. They are
- PHP Arithmetic Operators
- PHP Assignment Operators
- PHP Comparison Operators
- PHP Increment / Decrement Oeprators
- PHP Logical Operators
- PHP String Operators
- PHP Array Operators
- PHP Conditional Operators
Arithmetic Operators
PHP Arithmetic Operators perform mathematical arithmetic operations and return the result. Following are the arithmetic operators supported in PHP.
Arithmetic Operation | Operator Symbol | Example | Description |
---|---|---|---|
Addition | + | $x + $y | Returns sum of $x and $y. |
Subtraction | - | $x - $y | Returns difference of $y from $x. |
Multiplication | * | $x * $y | Returns product of $x and $y. |
Division | / | $x / $y | Returns the quotient when $x is divided by $y. |
Modulus | % | $x % $y | Returns reminder when $x is divided by $y. |
Exponentiation | ** | $x ** $y | Returns the result of $x raised to the power $y. |
Tutorials
Assignment Operators
PHP Assignment Operators are used to optionally perform an action with given operands and assign the result back to given variable (left operand).
The following table specifies symbol, example, and description for each of the Assignment Operator in PHP.
Assignment Operation | Operator Symbol | Example | Description |
---|---|---|---|
Simple Assignment | = | $x = 2 | Assign $x with 2. |
Addition Assignment | += | $x += 3 | Add 3 to the value of $x and assign the result to $x. |
Subtraction Assignment | -= | $x -= 3 | Subtract 3 from $x and assign the result to $x. |
Multiplication Assignment | *= | $x *= 3 | Multiply $x with 3 and assign the result to $x. |
Division Assignment | /= | $x /= 3 | Divide $x with 3 and assign the quotient to $x. |
Modulus Assignment | %= | $x %= 3 | Divide $x with 3 and assign the remainder to $x. |
Tutorials
- PHP Simple Assignment
- PHP Addition Assignment
- PHP Subtraction Assignment
- PHP Multiplication Assignment
- PHP Division Assignment
- PHP Modulus Assignment
Comparison Operators
Comparison Operators are used to compare two operands if they are equal, greater, lesser, etc.
The following table gives more information about these Comparison Operators in PHP.
Relational Operation | Operator Symbol | Example | Description |
---|---|---|---|
Equal | == | $x == $y | Returns true if $x is equal to $y, else false. |
Identical | === | $x === $y | Returns true if $x is equal in value to $y, and if $x and $y are of the same datatype. Otherwise returns false. |
Not equal | != | $x != $y | Returns true if $x is not equal to $y in value, else false. |
Not equal | <> | $x <> $y | Returns true if $x is not equal to $y in value, else false. |
Not identical | !== | $x !== $y | Returns true if $x is not equal in value to $y, or if $x and $y are of not the same datatype. Otherwise returns false. |
Greater than | > | $x > $y | Returns true if $x is greater than $y, else false. |
Less than | < | $x < $y | Returns true if $x is less than $y, else false. |
Greater than or equal | >= | $x >= $y | Returns true if $x is greater than or equal to $y, else false. |
Less than or equal | <= | $x <= $y | Returns true if $x is less than or equal to $y, else false. |
Spaceship | <=> | $x <=> $y | Returns an integer. Less than zero if $x less than $y. Equal to zero if $x is equal to $y. Greater than zero if $x is greater than $y. |
Tutorials
- PHP Equal To
- PHP Identical
- PHP Not Equal
- PHP Not Identical
- PHP Greater Than
- PHP Less Than
- PHP Greater Than or Equal To
- PHP Less Than or Equal To
- PHP Spaceship
Increment / Decrement Operators
PHP Arithmetic Operators perform mathematical arithmetic operations and return the result. Following are the arithmetic operators supported in PHP.
Arithmetic Operation | Operator Symbol | Example | Description |
---|---|---|---|
Pre-Increment | ++ | ++$x | Increments the value of $x by one. The value of $x is updated during the execution of this statement itself. |
Post-Increment | ++ | $x++ | Increments the value of $x by one. The value of $x is updated, only after the execution moves to next statement. |
Pre-Decrement | -- | --$x | Decrements the value of $x by one. The value of $x is updated during the execution of this statement itself. |
Post-Decrement | -- | $x-- | Decrements the value of $x by one. The value of $x is updated, only after the execution moves to next statement. |
Tutorials
- PHP Pre-Increment
- PHP Post-Increment
- PHP Pre-Decrement
- PHP Post-Decrement
Logical Operators
Logical Operators are used to create boolean conditions, modify a boolean expression, or combine two or more simple conditions to form a complex condition.
Java supports the following Logical Operators.
Logical Operation | Operator Symbol | Example | Description |
---|---|---|---|
AND | and | $x and $y | Returns true if both $x and $y are true. |
OR | or | $x or $y | Returns true if at least one of $x, $y is true. |
XOR | xor | $x xor $y | Returns true if $x and $y are different boolean values. |
AND | && | $x and $y | Returns true if both $x and $y are true. |
OR | || | $x or $y | Returns true if at least one of $x, $y is true. |
NOT | ! | !x | Returns true if $x is false, and vice versa. |
Tutorials
- PHP AND
- PHP OR
- PHP NOT
String Operators
There are two string operators in PHP.
Operator Name | Operator Symbol | Example | Description |
---|---|---|---|
Concatenation | . | $x . $y | Returns the concatenation of $x and $y. |
Concatenation Assignment | .= | $x .= $y | Concatenates $x and $y, and assigns the result back to $x. |
Tutorials
- PHP String Concatenation
- PHP String Concatenation Assignment
Array Operators
There are six array operators in PHP.
Relational Operation | Operator Symbol | Example | Description |
---|---|---|---|
Union | + | $x + $y | Returns an array created from the union of elements from $x and $y. |
Equal | == | $x == $y | Returns true if $x and $y have same values at every index. |
Identical | === | $x === $y | Returns true if $x and $y have same values and type at every index. |
Not equal | != | $x != $y | Returns true if $x and $y are not equal. |
Not equal | <> | $x <> $y | Returns true if $x and $y are not equal. |
Not identical | !== | $x !== $y | Returns true if $x and $y are not equal in value and type. |
Tutorials
- PHP Array Union
- PHP Array Equal
- PHP Array Identical
- PHP Array Not Equal
- PHP Array Not Identical
Conditional Assignment Operators
There are two conditional assignment operators in PHP.
Operator Name | Operator Symbol | Example | Description |
---|---|---|---|
Ternary | ?: | condition? value1: value2 | Returns value1 if condition is true, else value2 if condition is false. |
Null Coalescing | ?? | value1?? value2 | Returns value1 if value1 exists and not NULL, otherwise returns value2 . |
Tutorials
- PHP Ternary
- PHP Null Coalescing
Conclusion
In this PHP Tutorial, we learned about different operators in PHP, with exclusive tutorials for each of the operators.