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.
To help you learn, PHP operators are divided into the following categories:
Used with numeric values to perform common mathematical operations, like addition or subtraction.
Used with numeric values to write a value to a variable (e.g., = or +=).
Used to compare two values (number or string) and return a boolean result.
Used to combine conditional statements (e.g., and, or, not).
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
?>
Beyond math and logic, PHP includes several specialized operators that make your code more concise:
. and .=).? :) and the Null Coalescing operator (??).?? (Null Coalescing) are powerful modern features of PHP.