Interview frequency: …
Short answer
?? provides a fallback when a variable is null or undefined, while ?-> safely accesses methods and properties, stopping the chain if an object is null instead of throwing an error.
Explanation
Checking a nested object used to require several conditions, such as isset($user) && isset($user->profile). In PHP 8, ?-> shortens this to $user?->profile?->getAddress(); if any part of the chain is null, evaluation stops and returns null. ?? provides a default value: $name = $user?->name ?? 'Guest'.
Code example
$country = $order?->customer?->address?->getCountry() ?? "Unknown";Common mistakes
- Using ?-> to write or assign properties ($user?->name = 'Ali'), which PHP does not support.