Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Conditionals  ›  Capstone Project

The Null Operators: ?? and ?->

Null Coalescing Operator 14 minThe null coalescing operator ?? gives a fallback when a value is missing or null without a warning, and the nullsafe operator ?-> stops an object chain and returns null at the first null.

The Null Operators: ?? and ?->

Pageturn's search page reads ?page=2 from the address bar, but on the first visit there is no page at all. Elsewhere, an order may or may not have a customer record attached. PHP code spends a lot of time asking "is this missing?", and two short operators answer that neatly.

The rule

?? supplies a fallback for missing; ?-> stops safely at null.

The null coalescing operator ?? returns its left side if it exists and is not null, otherwise its right side:

<?php
$page = $_GET["page"] ?? 1;          // 1 when page is absent
$subtitle = $book["subtitle"] ?? "";  // "" when there is no subtitle

Crucially, ?? does not warn about a missing key or variable: it checks the left side like isset(), then picks. That makes it the standard way to read optional input.

It checks only for null or missing. A value of 0, "" or false is kept:

$discount = 0;
echo $discount ?? 10;   // 0: zero is a real value
echo $discount ?: 10;   // 10: ?: falls back on anything falsy

The assignment form ??= sets a variable only if it is currently null or missing: $name ??= "Guest";.

The nullsafe operator ?-> (PHP 8.0) is for objects, a topic beyond this course. You read an object's property or call its method with ->, as in $order->customer. If $order is null, reading a property gives a warning and calling a method is a fatal error. With ?->, the whole chain stops and gives null instead:

$email = $order?->customer?->email;   // null if order or customer is null

Combine the two for a display value: $order?->customer?->email ?? "no email".

The shortcut

Ask "could this be missing, and what should I use then?"

  • Missing array key or variable → $x ?? fallback.
  • A step in an object chain might be null → ?-> on that step.
  • A real 0 or "" must count as missing → ?:, not ??.
Reading the page number safelyStep 1 of 4
  1. The risk

    $_GET["page"] does not exist on the first visit. Reading it directly gives a warning and null.

Drills

Answer fast — trust the rule.

quick drill

With $discount = 0;, what is $discount ?? 10?

quick drill

What does $_GET["sort"] ?? "title" do when sort is not in the URL?

quick drill

If $order is null, what is $order?->customer?->email?

quick drill

Which line sets $name to "Guest" only if it is null or unset?

quick drill

A form field may arrive as an empty string, which should count as missing. Which operator gives a fallback for it?

Remember

  • ?? supplies a fallback for missing; ?-> stops safely at null.
  • Shortcut: ask "could this be missing, and what then?"
  • Trap: ?? keeps 0, "" and false; use ?: when those should count as missing.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

Explain?? or ?:

A discount field may be missing from the request, or present as 0. Explain what $_POST['discount'] ?? 10 and $_POST['discount'] ?: 10 each give in both situations, and which one Pageturn should use.