Is there optional chaining in PHP?

Is there optional chaining in PHP?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Is there optional chaining in PHP?

The short answer is no. As of today, PHP does not have a built-in syntax for optional chaining comparable to JavaScript's ?. operator. You cannot write $a->b->c and have the language automatically short-circuit if $a or $b is null. The community has debated adding this feature for years, often citing the verbosity of existing null checks as a pain point. However, implementing such a feature would fundamentally change how PHP handles object evaluation, which runs counter to the language's strict typing and error reporting philosophy.

The pattern you provided illustrates exactly why developers want this feature. In JavaScript, const d = a?.b?.c; is concise and readable. In PHP, achieving the same logic requires verbose conditionals that clutter your code:

if ($a && $a->b && $a->b->c) {
    $d = $a->b->c;
}

While this works, it forces you to evaluate the property twice if you were to refactor it slightly, or at least repeat the navigation logic. It also makes the code harder to scan quickly because the intent ("get c from b in a") is buried inside boolean logic.

Why PHP Resists Optional Chaining

To understand why this feature hasn't been implemented, we need to look at how PHP handles null and properties. Unlike JavaScript, where accessing a property on null typically results in a runtime error or returns null depending on the version, PHP is strict about object state. If you attempt to access a property on a non-existent variable or null, PHP throws an Error (or Notice in older versions) immediately.

This behavior enforces data integrity at compile time and runtime. Allowing optional chaining would require the engine to suppress these errors silently during evaluation. While some developers argue this improves developer experience by reducing boilerplate, others worry it obscures bugs. If a property is intentionally missing but your code treats it as "undefined" rather than an error, you might end up propagating null values through your application logic without realizing it, leading to subtle failures downstream.

Practical Alternatives in Pure PHP

Since native syntax isn't available yet, experienced PHP developers usually rely on helper functions or specific language features to achieve similar results without rewriting the core parser.

The most common approach is creating a utility method that mimics the behavior. This keeps your logic clean while maintaining explicit control over error handling:

function get($obj, $property) {
    if ($obj === null || !isset($obj->$property)) {
        return null;
    }
    return $obj->$property;
}

// Usage
$d = get(get(get($a, 'b'), 'c'));

This approach is verbose but explicit. It ensures that if a chain breaks, you know exactly why (the helper returned null). However, chaining multiple levels like this can still become nested and hard to read quickly.

Another strategy involves leveraging the ?-> syntax introduced in PHP 8.3 for method calls. While it doesn't solve property access ($a->b), it does handle methods on potentially null objects:

// Available in PHP 8.3+
$d = $a?->get($b)->get($c);

If $a is null, the method chain stops there and returns null. This is a significant step forward for method chaining but falls short of full property traversal. If you need to access nested properties on objects that might be null, you still face the syntax gap until PHP officially supports it.

The Laravel Ecosystem Context

If you are working within the Laravel framework, you have access to tools that abstract this complexity away. While Laravel itself doesn't add a new operator to the language, its collection facade provides robust methods for handling nested data safely.

$d = collect($a)->get('b')->get('c');

The collect method returns a Carbon-compatible collection object (actually Illuminate\Support\Collection) which implements double-underscore magic methods. Accessing 'b' on a collection containing an array or another collection will return the nested value, or null if it doesn't exist. This effectively acts as optional chaining for arrays and collections without requiring new syntax.

For pure object graphs that aren't wrapped in collections, the consensus remains that you must either accept the verbosity of if ($a && ...) or build your own helper classes. The trade-off is clear: gaining conciseness often comes at the cost of losing the explicit error signals that PHP's strict mode provides. Until the language specification evolves to accommodate silent property skipping, these workarounds remain the industry standard.

Our network