Lesson 4 of 20

Operators

PHP Operators

PHP supports arithmetic, comparison, logical, string, and special operators.

Example
<?php
// Arithmetic
$sum = 10 + 3;   // 13
$mod = 10 % 3;   // 1
$pow = 2 ** 8;   // 256 (exponentiation)

// String concatenation (dot operator)
$first = "Hello";
$last = "World";
$full = $first . " " . $last;  // "Hello World"
$full .= "!";                   // "Hello World!"

// Comparison
var_dump(5 == "5");   // true (loose comparison)
var_dump(5 === "5");  // false (strict — type must match)
var_dump(5 <=> 3);    // 1 (spaceship operator: -1, 0, or 1)

// Null coalescing
$user = $_GET['user'] ?? 'Guest';  // 'Guest' if not set

// Null safe operator (PHP 8)
$country = $user?->getAddress()?->getCountry();
?>