Operators are symbols that perform actions on variables and values. In PHP, the most commonly used operators are Arithmetic, Comparison, Logical, and Assignment operators.
Arithmetic operators are used for mathematical calculations.
+ → Addition- → Subtraction* → Multiplication/ → Division% → Modulus (remainder)<?php
$x = 10;
$y = 3;
echo "Addition: " . ($x + $y); // 13
echo " Subtraction: " . ($x - $y); // 7
echo " Multiplication: " . ($x * $y); // 30
echo " Division: " . ($x / $y); // 3.333...
echo " Modulus: " . ($x % $y); // 1
?>
Comparison operators are used to compare two values. They return either true or false.
== → Equal to!= → Not equal to> → Greater than< → Less than>= → Greater than or equal to<= → Less than or equal to<?php
$a = 5;
$b = 8;
var_dump($a == $b); // false
var_dump($a != $b); // true
var_dump($a > $b); // false
var_dump($a < $b); // true
?>
Logical operators are used to combine conditions.
&& → AND (true if both are true)|| → OR (true if at least one is true)! → NOT (reverses the result)<?php
$age = 20;
$has_id = true;
if($age >= 18 && $has_id){
echo "You can enter.";
} else {
echo "Access denied.";
}
?>
Assignment operators are used to assign values to variables.
= → Assign+= → Add and assign-= → Subtract and assign*= → Multiply and assign/= → Divide and assign<?php
$num = 10;
$num += 5; // $num = 15
$num -= 3; // $num = 12
$num *= 2; // $num = 24
$num /= 4; // $num = 6
echo $num;
?>