HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

PHP Operators

Written by RedoHub Team · Last updated: August 1, 2026

Operators are used to perform operations on variables and values. In PHP, operators are grouped into several categories based on the type of operation they perform—from simple math to complex logical evaluations.


Operator Groups

To help you learn, PHP operators are divided into the following categories:

Arithmetic

Used with numeric values to perform common mathematical operations, like addition or subtraction.

Assignment

Used with numeric values to write a value to a variable (e.g., = or +=).

Comparison

Used to compare two values (number or string) and return a boolean result.

Logical

Used to combine conditional statements (e.g., and, or, not).


Basic Syntax

Most operators work with two values (operands). For example, in the expression 5 + 10, 5 and 10 are operands, and + is the operator.

<?php
    $x = 10;
    $y = 4;

    echo $x + $y; // Math operator
    echo ($x == $y); // Comparison operator
?>

Specialized Operators

Beyond math and logic, PHP includes several specialized operators that make your code more concise:

  • String Operators: Specifically for joining strings (. and .=).
  • Array Operators: For comparing and merging arrays.
  • Conditional Assignment: Such as the Ternary operator (? :) and the Null Coalescing operator (??).
Important: Operands can be literal values (like 5) or variables (like $x). PHP will handle the underlying data types automatically during the operation.
Learning Path: We will explore each of these operator groups in detail in the following lessons. Mastering these is the key to writing functional PHP logic.

Key Points to Remember

  • Operators are symbols used to manipulate variables and values.
  • There are seven main groups of operators in PHP.
  • Arithmetic handles math; **Comparison** handles logic (true/false).
  • Assignment operators store values in variables.
  • Operators like ?? (Null Coalescing) are powerful modern features of PHP.

Common Mistakes to Avoid

Using + to join strings. Coming from JavaScript, it's natural to reach for + — but in PHP, + is strictly numeric addition. Use the dot operator . to concatenate strings instead.
Using = instead of == in a condition. if ($ready = true) assigns true to $ready and the condition always passes — it doesn't compare anything.
Relying on == for important comparisons. Loose equality performs type juggling that can produce surprising results with mixed types. Prefer === unless you specifically want type coercion.

Try It: The . vs + Trap

This is the single most common operator mistake for developers switching from JavaScript to PHP:

<?php
    $first = "Hello";
    $second = "World";

    echo $first + $second;  // 0  (both treated as numbers, both are 0)
    echo $first . " " . $second; // Hello World (correct concatenation)
?>

Frequently Asked Questions

Q: Does + concatenate strings in PHP?

A: No — + is always numeric addition in PHP. Use the dot operator . (or .= to append) for string concatenation.

Q: What's the difference between == and ===?

A: == compares values after converting types if needed (loose); === requires both the value and the type to match (strict). Default to === unless you have a specific reason not to.

Q: What does the ?? operator do?

A: The null coalescing operator — $name = $input ?? "Guest"; uses $input if it's set and not null, otherwise falls back to "Guest". See the PHP manual's operators reference for the full list.