What is the nullsafe operator in PHP 8?
Why Interviewers Ask This
Candidates at the intermediate level are expected to not only know this concept but explain the trade-offs involved. Interviewers use this question to see if you can reason about design decisions, not just recall facts.
Answer
The nullsafe operator (?->), introduced in PHP 8.0, allows you to safely chain method calls and property accesses on potentially null values. If any value in the chain is null, the entire expression short-circuits and returns null instead of throwing an error. Before PHP 8: $city = $user ? ($user->getAddress() ? $user->getAddress()->getCity() : null) : null;. With nullsafe operator: $city = $user?->getAddress()?->getCity();. It is conceptually similar to Kotlin's safe call operator (?.). The nullsafe operator can be combined with the null coalescing operator: $user?->getProfile()?->bio ?? "No bio";.
Common Mistake
Candidates often give textbook answers here. Interviewers are more impressed when you relate the concept to a specific problem you solved in a real PHP project.