🐘 PHP
Intermediate
What is the nullsafe operator in PHP 8?
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";.