?: operator (the 'Elvis operator') in PHP

?: operator (the 'Elvis operator') in PHP

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

Stefan Bogdanescu

Founder & Senior Architect

The Elvis Operator in PHP: Syntax, Semantics, and Pitfalls

You encounter $items = $items ?: $this->_handle->result('next', $this->_result, $this); and immediately suspect a typo or a misunderstanding of the ternary operator. Your intuition is correct regarding the syntax; it is a ternary operator, but it relies on a specific shorthand convention known as the "Elvis operator" because of its visual resemblance to Elvis Presley's signature mustache.

In standard PHP documentation, this construct isn't strictly named an operator like + or &&. It is simply a conditional expression where the consequence branch is omitted entirely. The logic follows a strict evaluation order: if the left operand ($items) evaluates to a "falsy" value, the right-hand side expression is executed and its result is assigned. If $items evaluates to any "truthy" value, the entire expression short-circuits, and that original value is used as-is.

How It Differs from Standard Ternary Logic

A standard ternary operator requires an explicit consequence: condition ? true_expr : false_expr. In your example, PHP implicitly treats a missing consequence branch as if you had written true_expr with the same type as the condition variable itself.

Think of it as a gatekeeper that only opens when the left side is empty. If $items contains data (truthy), the code ignores the complex object method call on the right and returns $items. Only when $items is null, false, an empty string, zero, or an empty array does PHP proceed to evaluate $this->_handle->result('next', ...).

This pattern is ubiquitous in legacy PHP codebases and quick prototypes because it reduces boilerplate. Instead of writing:

if (!$items) {
    $items = $this->_handle->result('next', $this->_result, $this);
}

You write the concise version shown earlier.

Falsy Values and Type Safety

The critical nuance here is understanding what constitutes a "falsy" value in PHP. The operator does not check for null exclusively. If $items is an empty string "", zero 0, or even an empty array [], the right-hand side will execute.

In modern application logic, relying on this behavior can introduce subtle bugs. For instance, if your API returns an empty array to signify "no results found," the Elvis operator will incorrectly attempt to fetch the next result set, treating the empty array as a failure state rather than valid data. To avoid this, developers often need to explicitly check for null:

$items = $items ?? $this->_handle->result('next', ...);

The null coalescing operator (??) introduced in PHP 7 is generally preferred over the Elvis operator for this specific purpose because it only triggers on null, preserving empty strings and arrays as valid, non-falsy values.

Trade-offs in Modern Codebases

While the Elvis operator saves characters, it sacrifices readability for those unfamiliar with the shorthand or the specific truthiness rules of PHP. In a large team environment or when maintaining older code (PHP 5.x), the ambiguity can lead to maintenance overhead where developers struggle to distinguish between intentional emptiness and a missing value.

If you are working within the Laravel ecosystem, which is frequently associated with clean, expressive syntax, you might find similar patterns in service providers or legacy controllers. However, even within the Laravel community, the consensus has shifted toward using ?? for null checks and explicit conditionals for other falsy values to ensure type safety.

Ultimately, recognizing the Elvis operator allows you to parse older codebases quickly without rewriting them immediately. But when writing new logic, opting for the explicit null coalescing operator or a standard if statement usually yields more maintainable and predictable results.

Our network