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 $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 (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 | true if $x equals $y |
=== | Identical (value and type) | $x === $y | true if $x is equal to $y and same type |
!= | Not equal | $x != $y | true if $x is not equal to $y |
!== | Not identical | $x !== $y | true if $x is not equal or not same type as $y |
> | Greater than | $x > $y | true if $x is greater than $y |
< | Less than | $x < $y | true if $x is less than $y |
>= | Greater than or equal | $x >= $y | true if $x is greater than or equal to $y |
<= | Less than or equal | $x <= $y | true if $x is less than or equal to $y |
PHP Logical Operators
Used to combine conditional statements.
Operator | Description | Example | Result |
---|---|---|---|
&& | Logical AND | $x && $y | true if both $x and $y are true |
` | ` | Logical OR | |
! | Logical NOT | !$x | true if $x is false |
PHP Increment/Decrement Operators
Used to increment or decrement a variable’s value.
Operator | Description | Example | Result |
---|---|---|---|
++$x | Pre-increment | ++$x | Increments $x by 1, then returns $x |
$x++ | Post-increment | $x++ | Returns $x , then increments $x by 1 |
--$x | Pre-decrement | --$x | Decrements $x by 1, then returns $x |
$x-- | Post-decrement | $x-- | Returns $x , then decrements $x by 1 |
PHP String Operators
Used to concatenate or manipulate strings.
Operator | Description | Example | Result |
---|---|---|---|
. | Concatenation | $x . $y | Joins $x and $y |
.= | Concatenate and assign | $x .= $y | Appends $y to $x |
PHP Array Operators
Used to compare and manipulate arrays.
Operator | Description | Example | Result |
---|---|---|---|
+ | Union | $x + $y | Combines $x and $y |
== | Equality | $x == $y | true if $x and $y have the same key-value pairs |
=== | Identity | $x === $y | true if $x and $y have the same key-value pairs and same order |
!= | Inequality | $x != $y | true if $x is not equal to $y |
<> | Inequality | $x <> $y | Same as != |
!== | Non-identity | $x !== $y | true if $x and $y are 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 | true if $obj is an instance of MyClass |
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 $y to $x if $y is 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
?>