In PHP, operators are symbols used to perform operations on variables and values. Understanding how operators work helps you write cleaner and more efficient code. PHP operators are mainly divided into arithmetic, comparison, logical, assignment, bitwise, and other special operators.
Arithmetic operators perform basic mathematical operations:
$a = 10;
$b = 3;
echo $a + $b; // Output 13
echo $a % $b; // Output 1 Comparison operators are used to compare two values and return a boolean result (true or false):
$a = 5;
$b = 10;
var_dump($a < $b); // true Logical operators are often used in conditional statements to combine multiple expressions:
$a = true;
$b = false;
var_dump($a && $b); // false Assignment operators assign values to variables, and can perform calculations simultaneously:
$x = 5;
$x += 3;
echo $x; // Output 8 Bitwise operators perform operations on binary representations of numbers. They are useful for low-level logic or permission handling:
$a = 5; // Binary 0101
$b = 3; // Binary 0011
echo $a & $b; // Output 1 Besides the main categories, PHP provides several special operators:
$score = 80;
$result = ($score >= 60) ? 'Pass' : 'Fail';
echo $result; // Output Pass Mastering PHP operators is essential for understanding program logic and controlling execution flow. With practice, these operators can make your code more efficient and readable.