What is Operators in PHP ?
PHP operators are symbols or combinations of symbols that are used to perform operations on variables and values. These operations include arithmetic calculations, comparisons, logical operations, and more. Operators are a fundamental part of PHP and enable you to manipulate data and build dynamic logic for your applications.
Types of PHP Operators
PHP provides a variety of operators grouped into the following categories:
- PHP Arithmetic Operators
- PHP Assignment Operators
- PHP Comparison Operators
- PHP Logical Operators
- PHP Increment/Decrement Operators
- PHP String Operators
- PHP Array Operators
- PHP Type Operators (introduced in PHP 7.0)
- PHP Null Coalescing Operator (introduced in PHP 7.0)
- PHP Spaceship Operator (introduced in PHP 7.0)
- PHP Error Control Operators
PHP Arithmetic Operators
Used to perform mathematical operations.
| Operator | Description | Example | Result | 
|---|---|---|---|
| + | Addition | $x + $y | Sum of $xand$y | 
| - | Subtraction | $x - $y | Difference of $xand$y | 
| * | Multiplication | $x * $y | Product of $xand$y | 
| / | Division | $x / $y | Quotient of $xand$y | 
| % | Modulus (remainder) | $x % $y | Remainder of $x / $y | 
PHP Assignment Operators
Used to assign values to variables.
| Operator | Description | Example | Equivalent To | 
|---|---|---|---|
| = | Assign | $x = $y | $x = $y | 
| += | Add and assign | $x += $y | $x = $x + $y | 
| -= | Subtract and assign | $x -= $y | $x = $x - $y | 
| *= | Multiply and assign | $x *= $y | $x = $x * $y | 
| /= | Divide and assign | $x /= $y | $x = $x / $y | 
| %= | Modulus and assign | $x %= $y | $x = $x % $y | 
PHP Comparison Operators
Used to compare two values.
| Operator | Description | Example | Result | 
|---|---|---|---|
| == | Equal | $x == $y | trueif$xequals$y | 
| === | Identical (value and type) | $x === $y | trueif$xis equal to$yand same type | 
| != | Not equal | $x != $y | trueif$xis not equal to$y | 
| !== | Not identical | $x !== $y | trueif$xis not equal or not same type as$y | 
| > | Greater than | $x > $y | trueif$xis greater than$y | 
| < | Less than | $x < $y | trueif$xis less than$y | 
| >= | Greater than or equal | $x >= $y | trueif$xis greater than or equal to$y | 
| <= | Less than or equal | $x <= $y | trueif$xis less than or equal to$y | 
PHP Logical Operators
Used to combine conditional statements.
| Operator | Description | Example | Result | 
|---|---|---|---|
| && | Logical AND | $x && $y | trueif both$xand$yare true | 
| ` | ` | Logical OR | |
| ! | Logical NOT | !$x | trueif$xis false | 
PHP Increment/Decrement Operators
Used to increment or decrement a variable’s value.
| Operator | Description | Example | Result | 
|---|---|---|---|
| ++$x | Pre-increment | ++$x | Increments $xby 1, then returns$x | 
| $x++ | Post-increment | $x++ | Returns $x, then increments$xby 1 | 
| --$x | Pre-decrement | --$x | Decrements $xby 1, then returns$x | 
| $x-- | Post-decrement | $x-- | Returns $x, then decrements$xby 1 | 
PHP String Operators
Used to concatenate or manipulate strings.
| Operator | Description | Example | Result | 
|---|---|---|---|
| . | Concatenation | $x . $y | Joins $xand$y | 
| .= | Concatenate and assign | $x .= $y | Appends $yto$x | 
PHP Array Operators
Used to compare and manipulate arrays.
| Operator | Description | Example | Result | 
|---|---|---|---|
| + | Union | $x + $y | Combines $xand$y | 
| == | Equality | $x == $y | trueif$xand$yhave the same key-value pairs | 
| === | Identity | $x === $y | trueif$xand$yhave the same key-value pairs and same order | 
| != | Inequality | $x != $y | trueif$xis not equal to$y | 
| <> | Inequality | $x <> $y | Same as != | 
| !== | Non-identity | $x !== $y | trueif$xand$yare not identical | 
PHP Type Operators (PHP 7.0+)
Used to check the type of an object.
| Operator | Description | Example | Result | 
|---|---|---|---|
| instanceof | Checks if an object is an instance of a class | $obj instanceof MyClass | trueif$objis an instance ofMyClass | 
PHP Null Coalescing Operator (PHP 7.0+)
Used to provide a default value for null variables.
| Operator | Description | Example | Result | 
|---|---|---|---|
| ?? | Null coalescing | $x = $y ?? $z | Assigns $yto$xif$yis not null; otherwise assigns$z | 
PHP Error Control Operators
Suppresses error messages.
| Operator | Description | Example | 
|---|---|---|
| @ | Error control | @file('abc') | 
Practical Example
Here’s how operators can be used together in PHP:
<?php
$x = 10;
$y = 5;
// Arithmetic
echo $x + $y; // Output: 15
// Comparison
echo $x > $y; // Output: 1 (true)
// Logical
if ($x > $y && $y > 0) {
    echo "Both conditions are true!";
}
// String
$name = "John";
$greeting = "Hello, " . $name;
echo $greeting; // Output: Hello, John
// Null Coalescing
$z = $undefinedVariable ?? 'Default Value';
echo $z; // Output: Default Value
?>
