PHP Operators

PHP Operators
Operators in PHP perform operations on variables and values. The main types of operators are:
Arithmetic Operators
Comparison Operators
Logical Operators
Assignment Operators
1. Arithmetic Operators
Used for mathematical calculations.
Operator Symbol Example ($a = 10, $b = 5) Output
Addition + $a + $b 15
Subtraction - $a - $b 5
Multiplication * $a * $b 50
Division / $a / $b 2
Modulus (Remainder) % $a % $b 0
Exponentiation ** $a ** $b 100000
<?php
$a = 10;
$b = 5;
echo $a + $b; // Output: 15
?>
Operator Symbol Example ($a = 10, $b = "10") Output
Equal == $a == $b true
Identical (Same Type) === $a === $b false
Not Equal != or <> $a != $b false
Not Identical !== $a !== $b true
Greater Than > $a > $b false
Less Than < $a < $b false
Greater or Equal >= $a >= $b true
Less or Equal <= $a <= $b true
<?php
$a = 10;
$b = "10";
var_dump($a == $b); // true (same value)
var_dump($a === $b); // false (different type)
?>
Operator Symbol Example ($x = true, $y = false) Output
AND && $x && $y false
AND (alternative) and $x and $y false
OR ` `
OR (alternative) or $x or $y true
NOT ! !$x false
XOR xor $x xor $y true
<?php
$x = true;
$y = false;
var_dump($x && $y); // false
var_dump($x || $y); // true
var_dump(!$x); // false
?>
Operator Symbol Example ($a = 10) Equivalent To
Assign = $a = 10 $a = 10
Add & Assign += $a += 5 $a = $a + 5
Subtract & Assign -= $a -= 5 $a = $a - 5
Multiply & Assign *= $a *= 5 $a = $a * 5
Divide & Assign /= $a /= 5 $a = $a / 5
Modulus & Assign %= $a %= 5 $a = $a % 5
<?php
$a = 10;
$a += 5; // Same as $a = $a + 5;
echo $a; // Output: 15
?>
Date: 2025-03-28 00:00:00.000000