Nullsafe Operator and Union Types
Use ?-> to safely chain nullable objects and declare union type hints.
Nullsafe Operator and Union Types is a free PHP Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Nullsafe Operator
PHP 8.0 introduced ?->. If the left-hand side is null, the entire chain short-circuits and returns null instead of throwing an error.
Problem it Solves
Without nullsafe, chaining nullable objects requires verbose null checks.
<?php
// Old way:
$city = null;
if ($user !== null && $user->getAddress() !== null) {
$city = $user->getAddress()->getCity();
}
// PHP 8 nullsafe:
$city = $user?->getAddress()?->getCity();Nullsafe with Method Calls
Each ?-> in the chain is checked independently. The first null short-circuits the rest.
<?php
$name = $order?->getCustomer()?->getName() ?? "Guest";Nullsafe with Properties
Works with property access too.
<?php
$zip = $user?->address?->zip;Nullsafe Does Not Skip Side Effects
If the nullsafe chain short-circuits, subsequent method calls in the chain are not executed — no side effects fire.
Union Types
PHP 8.0 union types let you declare that a parameter, property, or return value accepts one of several types.
<?php
function formatId(int|string $id): string {
return (string) $id;
}Union Type Examples
Common union types in practice:
<?php
function process(int|float $amount): float {
return (float) $amount;
}
function findUser(int|string $idOrEmail): ?User {
// search by ID or email
}Nullable Types
?Type is shorthand for Type|null — the value may be that type or null.
<?php
function greet(?string $name): string {
return "Hello, ".($name ?? "stranger");
}null in Union Types
You can include null explicitly in a union type: int|string|null.
Intersection Types (PHP 8.1)
PHP 8.1 added intersection types (A&B) — the value must satisfy all types simultaneously. Useful for type-safe dependency injection.
<?php
function process(Iterator&Countable $collection): void {
// $collection must be both Iterator AND Countable
}Type Coercion vs strict_types
Without declare(strict_types=1), PHP may coerce values (e.g. "42" to 42). With strict mode, only exact type matches are accepted.
Summary
The nullsafe operator (?->) eliminates repetitive null checks in chains. Union types (int|string) allow flexible yet type-safe function signatures.
Quick Check
What does the nullsafe operator return if a method in the chain returns null?
Frequently asked questions
Is the “Nullsafe Operator and Union Types” lesson free?
Yes — the full text of “Nullsafe Operator and Union Types” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Nullsafe Operator and Union Types”?
Use ?-> to safely chain nullable objects and declare union type hints. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Nullsafe Operator and Union Types” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Match Expression and Named Arguments
- Nullsafe Operator and Union Types
- Enumerations (Enums)
- Fibers: Cooperative Concurrency