What is the null coalescing operator (??) and the nullsafe operator (?->), and when would you use each?
Last updated
??returns the left side unless it's null or undefined, then the right side (a warning-free default).??=assigns only if currently null or unset.?->(PHP 8.0+) accesses a property or method only if the object isn't null, otherwise the whole chain returns null.Use
??for fallback values and?->to navigate possibly-null object chains. They combine:$user?->getName() ?? 'Anonymous'.