Two PHP 8 features that make code more readable with less boilerplate.
// ── Array Destructuring ──────────────────────────────────────────
$coordinates = [48.8566, 2.3522, 'Paris'];
[$lat, $lng, $city] = $coordinates;
// $lat = 48.8566, $lng = 2.3522, $city = 'Paris'
// Skip elements with commas
[, , $cityOnly] = $coordinates;
// Nested destructuring
$users = [['Alice', 30], ['Bob', 25]];
foreach ($users as [$name, $age]) {
echo "{$name} is {$age} years old\n";
}
// ── Named Arguments ──────────────────────────────────────────────
// Before PHP 8 — positional, easy to mix up
array_slice($array, 0, 5, true);
// PHP 8+ — self-documenting
array_slice(array: $array, offset: 0, length: 5, preserve_keys: true);
// Great for functions with optional middle parameters
htmlspecialchars(string: $html, double_encode: false);
All Comments