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

Used to perform mathematical operations.

OperatorDescriptionExampleResult
+Addition$x + $ySum of $x and $y
-Subtraction$x - $yDifference of $x and $y
*Multiplication$x * $yProduct of $x and $y
/Division$x / $yQuotient of $x and $y
%Modulus (remainder)$x % $yRemainder of $x / $y

PHP Assignment Operators

Used to assign values to variables.

OperatorDescriptionExampleEquivalent 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.

OperatorDescriptionExampleResult
==Equal$x == $ytrue if $x equals $y
===Identical (value and type)$x === $ytrue if $x is equal to $y and same type
!=Not equal$x != $ytrue if $x is not equal to $y
!==Not identical$x !== $ytrue if $x is not equal or not same type as $y
>Greater than$x > $ytrue if $x is greater than $y
<Less than$x < $ytrue if $x is less than $y
>=Greater than or equal$x >= $ytrue if $x is greater than or equal to $y
<=Less than or equal$x <= $ytrue if $x is less than or equal to $y

PHP Logical Operators

Used to combine conditional statements.

OperatorDescriptionExampleResult
&&Logical AND$x && $ytrue if both $x and $y are true
` `Logical OR
!Logical NOT!$xtrue if $x is false

PHP Increment/Decrement Operators

Used to increment or decrement a variable’s value.

OperatorDescriptionExampleResult
++$xPre-increment++$xIncrements $x by 1, then returns $x
$x++Post-increment$x++Returns $x, then increments $x by 1
--$xPre-decrement--$xDecrements $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.

OperatorDescriptionExampleResult
.Concatenation$x . $yJoins $x and $y
.=Concatenate and assign$x .= $yAppends $y to $x

PHP Array Operators

Used to compare and manipulate arrays.

OperatorDescriptionExampleResult
+Union$x + $yCombines $x and $y
==Equality$x == $ytrue if $x and $y have the same key-value pairs
===Identity$x === $ytrue if $x and $y have the same key-value pairs and same order
!=Inequality$x != $ytrue if $x is not equal to $y
<>Inequality$x <> $ySame as !=
!==Non-identity$x !== $ytrue if $x and $y are not identical

PHP Type Operators (PHP 7.0+)

Used to check the type of an object.

OperatorDescriptionExampleResult
instanceofChecks if an object is an instance of a class$obj instanceof MyClasstrue if $obj is an instance of MyClass

PHP Null Coalescing Operator (PHP 7.0+)

Used to provide a default value for null variables.

OperatorDescriptionExampleResult
??Null coalescing$x = $y ?? $zAssigns $y to $x if $y is not null; otherwise assigns $z

PHP Error Control Operators

Suppresses error messages.

OperatorDescriptionExample
@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
?>