Operators in PHP
Arithmetic Operators
Section titled “Arithmetic Operators”Used for basic mathematical operations:
<?php$a = 10;$b = 3;
echo $a + $b; // 13 - Additionecho $a - $b; // 7 - Subtractionecho $a * $b; // 30 - Multiplicationecho $a / $b; // 3.333... - Divisionecho $a % $b; // 1 - Modulus (remainder)echo $a ** $b; // 1000 - Exponentiation (PHP 5.6+)Assignment Operators
Section titled “Assignment Operators”Used to assign values to variables:
<?php$x = 10; // Basic assignment$x += 5; // $x = $x + 5 → 15$x -= 3; // $x = $x - 3 → 12$x *= 2; // $x = $x * 2 → 24$x /= 4; // $x = $x / 4 → 6$x %= 5; // $x = $x % 5 → 1$x **= 2; // $x = $x ** 2 → 1Comparison Operators
Section titled “Comparison Operators”Used to compare two values:
<?php$a = 5;$b = "5";$c = 10;
var_dump($a == $b); // true - Equal (value)var_dump($a === $b); // false - Identical (value and type)var_dump($a != $b); // false - Not equalvar_dump($a !== $b); // true - Not identicalvar_dump($a < $c); // true - Less thanvar_dump($a > $c); // false - Greater thanvar_dump($a <= $c); // true - Less than or equal tovar_dump($a >= $c); // false - Greater than or equal to
// Spaceship operator (PHP 7+)echo 1 <=> 1; // 0 - Equalecho 1 <=> 2; // -1 - Less thanecho 2 <=> 1; // 1 - Greater thanLogical Operators
Section titled “Logical Operators”Used to combine conditional statements:
<?php$age = 25;$hasLicense = true;
// AND, OR operatorsif ($age >= 18 && $hasLicense) { echo "Can drive";}
if ($age < 18 || $age > 65) { echo "Special age group";}
// NOT operatorif (!$hasLicense) { echo "No driving license";}
// Alternative syntaxif ($age >= 18 and $hasLicense) { } // ANDif ($age < 18 or $age > 65) { } // ORif ($age >= 18 xor $hasLicense) { } // XOR (exclusive OR)String Operators
Section titled “String Operators”Used for string manipulation:
<?php$str1 = "Hello";$str2 = "World";
// Concatenation$greeting = $str1 . " " . $str2; // "Hello World"
// Concatenation assignment$str1 .= " PHP"; // $str1 becomes "Hello PHP"Increment/Decrement OperatorsUsed to increase or decrease values:<?php$x = 5;
echo ++$x; // 6 - Pre-incrementecho $x++; // 6 - Post-increment (then $x becomes 7)echo --$x; // 6 - Pre-decrementecho $x--; // 6 - Post-decrement (then $x becomes 5)Array Operators
Section titled “Array Operators”Used to work with arrays:
<?php$arr1 = [1, 2];$arr2 = [3, 4];$arr3 = [1, 2];
// Union$union = $arr1 + $arr2; // [1, 2] (keys preserved)
// Equalityvar_dump($arr1 == $arr3); // true - Same key/value pairsvar_dump($arr1 === $arr3); // true - Same key/value pairs in same ordervar_dump($arr1 != $arr2); // true - Not equalvar_dump($arr1 !== $arr2); // true - Not identicalTernary Operator
Section titled “Ternary Operator”Short conditional assignment:
<?php$age = 20;$status = ($age >= 18) ? "Adult" : "Minor";echo $status; // "Adult"Shorthand ternary (PHP 5.3+)
Section titled “Shorthand ternary (PHP 5.3+)”$name = "";$displayName = $name ?: "Guest"; // "Guest"Null Coalescing Operator (PHP 7+)Checks if value exists and is not null:<?php$username = $_GET['user'] ?? 'guest'; // If user doesn't exist, use 'guest'
// Null coalescing assignment (PHP 7.4+)$array = [];$array['key'] ??= 'default'; // Only sets if key doesn't existType Operators
Section titled “Type Operators”Used for type checking:
<?phpclass MyClass {}$obj = new MyClass();
var_dump($obj instanceof MyClass); // true
class ParentClass {}class ChildClass extends ParentClass {}$child = new ChildClass();
var_dump($child instanceof ParentClass); // trueExecution Operator
Section titled “Execution Operator”Executes shell commands:
<?php$output = `ls -la`; // Backticks, not single quotesecho $output;