PHP Operators: The Ultimate Devyra Guide
In PHP, operators are symbols used to perform operations on variables and values. From basic arithmetic to complex conditional logic, understanding PHP operators is fundamental to writing efficient and clean code. This Devyra guide covers all types of PHP operators with examples and use cases.
Types of Operators in PHP
PHP groups its operators into the following categories:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Increment / Decrement Operators
- Logical Operators
- String Operators
- Array Operators
- Conditional Assignment Operators
1. PHP Arithmetic Operators
Used with numeric values to perform common mathematical operations.
Operator | Name | Example | Result |
---|---|---|---|
+ | Addition | $x + $y | Sum of $x and $y |
– | Subtraction | $x – $y | Difference of $x and $y |
* | Multiplication | $x * $y | Product of $x and $y |
/ | Division | $x / $y | Quotient of $x and $y |
% | Modulus | $x % $y | Remainder of $x divided by $y |
** | Exponentiation | $x ** $y | $x raised to the power of $y |
2. PHP Assignment Operators
Used to assign values to variables and optionally perform an operation in the process.
Operator | Equivalent | Description |
---|---|---|
= | $x = $y | Assigns $y to $x |
+= | $x = $x + $y | Adds $y to $x |
-= | $x = $x – $y | Subtracts $y from $x |
*= | $x = $x * $y | Multiplies $x by $y |
/= | $x = $x / $y | Divides $x by $y |
%= | $x = $x % $y | Modulus assignment |
3. PHP Comparison Operators
Used to compare two values and return a boolean result.
Operator | Name | Example | Result |
---|---|---|---|
== | Equal | $x == $y | True if values are equal |
=== | Identical | $x === $y | True if value and type are equal |
!= | Not equal | $x != $y | True if values differ |
<> | Not equal | $x <> $y | True if values differ |
!== | Not identical | $x !== $y | True if type or value differs |
> | Greater than | $x > $y | True if $x is greater |
< | Less than | $x < $y | True if $x is smaller |
>= | Greater or equal | $x >= $y | True if $x ≥ $y |
<= | Less or equal | $x <= $y | True if $x ≤ $y |
<=> | Spaceship | $x <=> $y | -1, 0, or 1 |
4. PHP Increment/Decrement Operators
Used to increase or decrease variable values.
++$x
: Pre-increment$x++
: Post-increment--$x
: Pre-decrement$x--
: Post-decrement
5. PHP Logical Operators
Used to combine conditional expressions.
Operator | Name | Example | Result |
---|---|---|---|
and | Logical AND | $x and $y | True if both are true |
or | Logical OR | $x or $y | True if one is true |
xor | Exclusive OR | $x xor $y | True if one is true, not both |
&& | Logical AND | $x && $y | Same as and |
|| | Logical OR | $x || $y | Same as or |
! | Logical NOT | !$x | True if $x is false |
6. PHP String Operators
Operator | Name | Example | Result |
---|---|---|---|
. | Concatenation | $txt1 . $txt2 | Joins two strings |
.= | Concatenation assignment | $txt1 .= $txt2 | Appends to original string |
7. PHP Array Operators
Used to compare or combine arrays.
Operator | Name | Example | Result |
---|---|---|---|
+ | Union | $x + $y | Combines arrays |
== | Equality | $x == $y | True if key-value pairs match |
=== | Identity | $x === $y | True if pairs match in order and type |
!= | Inequality | $x != $y | True if pairs differ |
<> | Inequality | $x <> $y | Same as != |
!== | Non-identity | $x !== $y | True if order/type differ |
8. PHP Conditional Assignment Operators
Operator | Name | Example | Description |
---|---|---|---|
?: | Ternary | $x = $a ? $b : $c | Assigns $b if $a is true, otherwise $c |
?? | Null coalescing | $x = $a ?? $b | Assigns $a if set and not null, otherwise $b |
Conclusion
PHP operators are the building blocks of decision-making and data manipulation in any PHP script. From basic arithmetic to complex logic, mastering these operators is key to writing clean, robust, and high-performing code. Follow Devyra for more deep-dive PHP tutorials that elevate your development skills to the next level.