What is the null coalescing operator (??) and the nullsafe operator (?->), and when would you use each?

Last updated 15 June 2026

  • ?? 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'.

  • https://www.tutorialslogic.com/php/conditional-statements
  • https://phptutorialpoints.in/php-8-features-and-improvements/
All PHP interview questions