PHP null-safe operator ?-> with null coalescing for safe property chains
PHP Dasun Kalupahana 89 views
<?php
class Address { public function __construct(public ?string $city = null) {} }
class User { public function __construct(public ?Address $address = null) {} }
$users = [new User(new Address('Paris')), new User(), null];
foreach ($users as $u) {
echo $u?->address?->city ?? 'Unknown', PHP_EOL;
}
// Paris
// Unknown
// Unknown
?-> stops the whole chain and returns null when any link is null, so you don’t need nested if checks. Add ?? for a default value. Needs PHP 8.0+.
Learn more on the blog
PHP
How to Detect and Fix PHP Memory Leaks
Last updated: 5 November 2026. See the update log at the end.In...
9 min read
Industry Trends
AI Coding Prompts That Actually Improve Productivity
Most lists of "best AI coding prompts" are collections of clever wording....
8 min read
Laravel
Laravel Herd Setup: Sites, PHP Versions, HTTPS and Databases
Laravel Herd is a local development environment for macOS and Windows that...
5 min read