Operators in PHP – A Complete Guide

Table of Contents

  • Introduction to PHP Operators
  • Types of PHP Operators
    • Arithmetic Operators
    • Assignment Operators
    • Comparison Operators
    • Increment/Decrement Operators
    • Logical Operators
    • String Operators
    • Array Operators
    • Spaceship and Null Coalescing Operators (PHP 7+)
  • Operator Precedence in PHP
  • Practical Examples
  • Summary

Introduction to PHP Operators

In PHP, operators are symbols or combinations of symbols that perform operations on variables and values. Whether you’re adding numbers, comparing values, or evaluating logic conditions, operators are essential building blocks in any PHP program.

Understanding how each type of operator works will help you write clean, efficient, and bug-free code.


Types of PHP Operators

Let’s break down each category of operators in PHP.


1. Arithmetic Operators

Used to perform mathematical operations.

OperatorDescriptionExample
+Addition$x + $y
-Subtraction$x - $y
*Multiplication$x * $y
/Division$x / $y
%Modulus (remainder)$x % $y
**Exponentiation (PHP 5.6+)$x ** $y

Example:

$x = 10;
$y = 3;
echo $x % $y; // Output: 1

2. Assignment Operators

Used to assign values to variables.

OperatorExampleEquivalent To
=$x = 5$x = 5
+=$x += 3$x = $x + 3
-=$x -= 2$x = $x - 2
*=$x *= 4$x = $x * 4
/=$x /= 2$x = $x / 2
%=$x %= 3$x = $x % 3

3. Comparison Operators

Used to compare values; results in a boolean (true or false).

OperatorDescriptionExample
==Equal (type-agnostic)$x == $y
===Identical (type-safe)$x === $y
!=Not equal$x != $y
<>Not equal (alt syntax)$x <> $y
!==Not identical$x !== $y
>Greater than$x > $y
<Less than$x < $y
>=Greater than or equal$x >= $y
<=Less than or equal$x <= $y

4. Increment/Decrement Operators

These operators increase or decrease the value of a variable.

OperatorDescriptionExample
++$xPre-incrementIncrement, then return value
$x++Post-incrementReturn value, then increment
--$xPre-decrementDecrement, then return value
$x--Post-decrementReturn value, then decrement

5. Logical Operators

Used to combine conditional statements.

OperatorDescriptionExample
andLogical AND$x and $y
orLogical OR$x or $y
xorLogical XOR$x xor $y
&&Logical AND$x && $y
``
!Logical NOT!$x

6. String Operators

Only two operators are used with strings in PHP.

OperatorDescriptionExample
.Concatenation$text = "Hello" . " World"
.=Concatenation & assign$text .= "!"

7. Array Operators

Used to compare or combine arrays.

OperatorDescriptionExample
+Union$a + $b
==Equal$a == $b
===Identical$a === $b
!=Not equal$a != $b
!==Not identical$a !== $b

8. Special Operators (PHP 7+)

Spaceship Operator (<=>)

Returns -1, 0, or 1 when comparing two expressions.

echo 5 <=> 10; // Output: -1

Null Coalescing Operator (??)

Returns the first value if it exists and is not null.

$name = $_GET['name'] ?? 'Guest';

Operator Precedence in PHP

When multiple operators are used, precedence determines the execution order.

For example:

$result = 10 + 5 * 2; // Outputs 20 (5*2 first, then +10)

Use parentheses to override default precedence:

$result = (10 + 5) * 2; // Outputs 30

Practical Examples

$a = 10;
$b = 5;
$c = 15;

if ($a > $b && $c > $a) {
echo "Both conditions are true";
}

$text = "Hello";
$text .= " World";
echo $text; // Outputs "Hello World"

Summary

In this module, you explored all major operators in PHP—from arithmetic and logical to advanced ones like null coalescing and spaceship operators. Operators form the logical and mathematical backbone of any PHP application.