In PHP, operators are special symbols used to perform operations on variables and values. They help you perform tasks such as mathematical calculations, comparisons, logical checks, and string manipulations.
- Operators help combine or modify values in PHP expressions.
- They make it easier to check conditions and control program flow.
- Different operators are used for different tasks, such as assigning values and comparing data.
PHP operators are divided into several categories:
Arithmetic Operators
Arithmetic operators are used to perform basic arithmetic operations like addition, subtraction, multiplication, division, and modulus.
| Operator | Name | Example | Description |
|---|---|---|---|
+ | Addition | $x + $y | Adds two values |
- | Subtraction | $x - $y | Subtracts one value from another |
* | Multiplication | $x * $y | Multiplies two values |
/ | Division | $x / $y | Divides one value by another |
% | Modulus | $x % $y | Returns the remainder |
** | Exponentiation | $x ** $y | Raises $x to the power of $y |
Example:
<?php
// Define two numbers
$x = 10;
$y = 3;
// Addition
echo "Addition: " . ($x + $y) . "\n";
// Subtraction
echo "Subtraction: " . ($x - $y) . "\n";
// Multiplication
echo "Multiplication: " . ($x * $y) . "\n";
// Division
echo "Division: " . ($x / $y) . "\n";
// Exponentiation
echo "Exponentiation: " . ($x ** $y) . "\n";
// Modulus
echo "Modulus: " . ($x % $y) . "\n";
?>
Logical Operators
Logical operators are used to operate with conditional statements. These operators evaluate conditions and return a boolean result (true or false).
Operator | Name | Syntax | Operation |
|---|---|---|---|
| and | Logical AND | $x and $y | True if both the operands are true else false |
| or | Logical OR | $x or $y | True if either of the operands is true otherwise, it is false |
| xor | Logical XOR | $x xor $y | True if either of the operands is true and false if both are true |
| && | Logical AND | $x && $y | True if both the operands are true else false |
| || | Logical OR | $x || $y | True if either of the operands is true otherwise, it is false |
| ! | Logical NOT | !$x | True if $x is false |
Example:
<?php
$x = 50;
$y = 30;
if ($x == 50 and $y == 30)
echo "and Success \n";
if ($x == 50 or $y == 20)
echo "or Success \n";
if ($x == 50 xor $y == 20)
echo "xor Success \n";
if ($x == 50 && $y == 30)
echo "&& Success \n";
if ($x == 50 || $y == 20)
echo "|| Success \n";
if (!$z)
echo "! Success \n";
?>
Comparison Operators
Comparison operators are used to compare two values and return a Boolean result (true or false).
| Operator | Name | Syntax | Operation |
|---|---|---|---|
| == | Equal To | $x == $y | Returns True if both the operands are equal |
| != | Not Equal To | $x != $y | Returns True if both the operands are not equal |
| <> | Not Equal To | $x <> $y | Returns True if both the operands are unequal |
| === | Identical | $x === $y | Returns True if both the operands are equal and are of the same type |
| !== | Not Identical | $x == $y | Returns True if both the operands are unequal and are of different types |
| < | Less Than | $x < $y | Returns True if $x is less than $y |
| > | Greater Than | $x > $y | Returns True if $x is greater than $y |
| <= | Less Than or Equal To | $x <= $y | Returns True if $x is less than or equal to $y |
| >= | Greater Than or Equal To | $x >= $y | Returns True if $x is greater than or equal to $y |
Note: Always prefer === and !== for safer comparisons.
Example:
<?php
$a = 80;
$b = 50;
$c = "80";
// Here var_dump function has been used to
// display structured information. We will learn
// about this function in complete details in further
// articles.
var_dump($a == $c) . "\n";
var_dump($a != $b) . "\n";
var_dump($a <> $b) . "\n";
var_dump($a === $c) . "\n";
var_dump($a !== $c) . "\n";
var_dump($a < $b) . "\n";
var_dump($a > $b) . "\n";
var_dump($a <= $b) . "\n";
var_dump($a >= $b);
?>
Conditional or Ternary Operators
These operators are used to compare two values and take either of the results simultaneously, depending on whether the outcome is TRUE or FALSE. These are also used as a shorthand notation for the if...else statement that we will read in the article on decision making.
Syntax:
$var = (condition)? value1 : value2;Returns $x if the condition is true; otherwise, returns $y.
Example:
<?php
$x = -12;
echo ($x > 0) ? 'The number is positive' : 'The number is negative';
?>
Assignment Operators
Assignment operators are used to assign values to variables. These operators allow you to assign a value and perform operations in a single step.
Operator | Name | Syntax | Operation |
|---|---|---|---|
= | Assign | $x = $y | Operand on the left obtains the value of the operand on the right |
+= | Add then Assign | $x += $y | Simple Addition same as $x = $x + $y |
-= | Subtract then Assign | $x -= $y | Simple subtraction same as $x = $x - $y |
*= | Multiply then Assign | $x *= $y | Simple product same as $x = $x * $y |
/= | Divide, then assign (quotient) | $x /= $y | Simple division same as $x = $x / $y |
%= | Divide, then assign (remainder) | $x %= $y | Simple division same as $x = $x % $y |
Example:
<?php
// Simple assign operator
$y = 75;
echo $y, "\n";
// Add then assign operator
$y = 100;
$y += 200;
echo $y, "\n";
// Subtract then assign operator
$y = 70;
$y -= 10;
echo $y, "\n";
// Multiply then assign operator
$y = 30;
$y *= 20;
echo $y, "\n";
// Divide then assign(quotient) operator
$y = 100;
$y /= 5;
echo $y, "\n";
// Divide then assign(remainder) operator
$y = 50;
$y %= 5;
echo $y;
?>
Array Operators
These operators are used in the case of arrays. Here are the array operators, along with their syntax and operations, that PHP provides for the array operation.
Operator | Name | Syntax | Operation |
|---|---|---|---|
+ | Union | $x + $y | Union of both, i.e., $x and $y |
== | Equality | $x == $y | Returns true if both have the same key-value pair |
!= | Inequality | $x != $y | Returns True if both are unequal |
=== | Identity | $x === $y | Returns True if both have the same key-value pair in the same order and of the same type |
!== | Non-Identity | $x !== $y | Returns True if both are not identical to each other |
Example:
<?php
$x = array("k" => "Car", "l" => "Bike");
$y = array("a" => "Train", "b" => "Plane");
var_dump($x + $y);
var_dump($x == $y) . "\n";
var_dump($x != $y) . "\n";# inequal
var_dump($x <> $y) . "\n";
var_dump($x === $y) . "\n";
var_dump($x !== $y) . "\n";
?>
Increment/Decrement Operators
These are called the unary operators as they work on single operands. These are used to increment or decrement values.
Operator | Name | Syntax | Operation |
|---|---|---|---|
| ++ | Pre-Increment | ++$x | First, increment $x by one, then return $x |
| -- | Pre-Decrement | --$x | First, decrement $x by one, then return $x |
| ++ | Post-Increment | $x++ | First returns $x, then increment it by one |
| -- | Post-Decrement | $x-- | First, return $x, then decrement it by one |
Example:
<?php
$x = 2;
echo ++$x, " First increments then prints \n";
echo $x, "\n";
$x = 2;
echo $x++, " First prints then increments \n";
echo $x, "\n";
$x = 2;
echo --$x, " First decrements then prints \n";
echo $x, "\n";
$x = 2;
echo $x--, " First prints then decrements \n";
echo $x;
?>
String Operators
This operator is used for the concatenation of 2 or more strings using the concatenation operator ('.'). We can also use the concatenating assignment operator ('.=') to append the argument on the right side to the argument on the left side.
Operator | Name | Syntax | Operation |
|---|---|---|---|
| . | Concatenation | $x.$y | Concatenated $x and $y |
| .= | Concatenation and assignment | $x.=$y | First, it concatenates then assigns, the same as $x = $x.$y |
Example:
<?php
$x = "Geeks";
$y = "for";
$z = "Geeks!!!";
echo $x . $y . $z, "\n";
$x .= $y . $z;
echo $x;
?>


