Nullable Types (?Type)
Also Known As
nullable type
?string
PHP nullable
TL;DR
Declaring a type as ?Type allows either a value of that type or null — shorthand for Type|null.
Explanation
Nullable types (PHP 7.1+) prefix a type declaration with ? to indicate the value may be null in addition to the declared type. function find(?int $id): ?User accepts null for $id and may return a User or null. This is equivalent to the union type int|null and User|null introduced in PHP 8.0. Nullable types made it practical to type-hint optional parameters and nullable return values without resorting to mixed. Combine with the null coalescing operator (??) and nullsafe operator (?->) for clean null handling.
Common Misconception
✗ Nullable types encourage passing null everywhere. They do the opposite — they make nullability explicit and opt-in. A function typed string cannot receive null; only ?string can. This forces developers to handle null intentionally rather than accidentally.
Why It Matters
PHP's nullable type syntax (?Type) makes null a documented, type-checked part of the contract — without it, null silently passes through typed parameters causing downstream failures.
Common Mistakes
- Using ?Type when null should actually be forbidden — nullable types are sometimes overused to avoid validation.
- Forgetting that nullable return types require explicit return null statements — returning nothing returns null implicitly but ?Type requires it to be intentional.
- Not using the nullsafe operator (?->) when chaining calls on nullable return values.
- Returning null from constructors — constructors cannot be nullable; throw an exception instead.
Code Examples
✗ Vulnerable
// Nullable parameter used where null should be invalid:
function setAge(?int $age): void {
$this->age = $age; // Allows null — but a person must have an age
}
// If null means 'unknown', model it explicitly:
function setAge(int $age): void { // Enforce valid age
if ($age < 0 || $age > 150) throw new InvalidArgumentException();
$this->age = $age;
}
✓ Fixed
// ?Type is shorthand for Type|null
function findUser(int \$id): ?User {
return \$this->db->find(\$id); // null if not found
}
// Nullable parameter with default
function greet(?string \$name = null): string {
return 'Hello, ' . (\$name ?? 'Guest');
}
// Typed nullable property (PHP 7.4+)
class Order {
public ?\DateTimeImmutable \$shippedAt = null; // not shipped yet
}
// PHP 8.0+ — use union type for explicit null
function process(int|null \$value): void {}
// Equivalent to: function process(?int \$value): void {}
References
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
15 Mar 2026
Edited
22 Mar 2026
Views
30
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Amazonbot 8
Perplexity 6
ChatGPT 3
Unknown AI 2
Ahrefs 2
SEMrush 2
Google 2
Also referenced
How they use it
crawler 23
crawler_json 2
Related categories
⚡
DEV INTEL
Tools & Severity
🟡 Medium
⚙ Fix effort: Low
⚡ Quick Fix
Use ?string for optional parameters and return types — but consider whether null is the right signal; sometimes an empty collection, a NullObject, or an Optional-style type communicates intent better
📦 Applies To
PHP 7.1+
web
cli
queue-worker
🔍 Detection Hints
Function returning null for 'not found' without nullable return type; mixed return type hiding nullability; getUser() returning null without ?User annotation
Auto-detectable:
✓ Yes
phpstan
psalm
rector
⚠ Related Problems
🤖 AI Agent
Confidence: Medium
False Positives: Medium
✓ Auto-fixable
Fix: Low
Context: Function