PHP short-ternary ("Elvis") operator vs null coalescing operator

PHP short-ternary ("Elvis") operator vs null coalescing operator

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

Stefan Bogdanescu

Founder & Senior Architect

PHP Short-Ternary vs Null Coalescing: When They Diverge

In PHP, selecting between two operators is often a matter of style, but treating ?: and ?? as interchangeable is a subtle bug waiting to happen. While both look like alternatives for providing fallback values, their execution logic differs fundamentally when dealing with "falsy" data types. Understanding the distinction requires looking past the concept of nullity into the realm of boolean truthiness in PHP.

The short-ternary operator (?:) acts as a conditional statement. It evaluates the left side; if that expression is strictly false, it returns the right side. In PHP, the boolean false, the integer 0, an empty string (""), and the string "0" are all considered falsy. Therefore, $a ?: $b will return $b not only when $a is null but also when $a is zero or an empty string.

Conversely, the null coalescing operator (??) is strictly about missingness. It returns the right side if and only if the left side is null. If you assign the number 0 to a variable and use it with ??, the operation stops there because 0 exists as a valid value; it does not trigger the fallback.

$a = 0;
$b = 'default';

// Short-ternary: Returns 'default' because 0 is falsy
echo ($a ?: $b); // Output: default

// Null coalescing: Returns 0 because the variable exists and is not null
echo ($a ?? $b); // Output: 0

This distinction creates specific pitfalls when handling numeric inputs. If you are validating an integer field from a database or a form, using ?: to provide a default might inadvertently overwrite a legitimate 0 value with your fallback string or number. This is a classic source of data corruption in financial applications or inventory systems where zero is a valid count.

The trade-off lies in the semantic intent of your code. Use ?? when you are asking, "Did this variable exist at all?" This is appropriate for optional configuration options or user preferences that might be unset. Use ?: when you are asking, "Is there any value here worth using?" This pattern often appears in legacy codebases where developers intended to treat empty strings as equivalent to null, a behavior that persists but requires explicit acknowledgment.

Modern PHP versions, particularly 8.0 and later, have introduced the null coalescing assignment operator (??=). This allows you to set a default value only if the current one is null, without creating an intermediate variable. This feature reinforces the preference for ?? in new codebases where strict null checks are desired rather than loose truthiness checks.

When integrating these concepts into larger frameworks like Laravel, the logic remains consistent regardless of the underlying engine version. However, be wary of mixing these operators in ternary chains. A common mistake involves nesting a short-ternary inside a null coalescing check:

// Incorrect logic for intended behavior
$value = $input ?? ($short ?: $long); 

In this scenario, if $input is null, the code proceeds to evaluate $short ?: $long. If $short is 0, it will still return $long, which might not be the desired fallback chain. Developers sometimes assume that because they are using a fallback operator, any falsy value should trigger the next step, but PHP distinguishes between "empty" and "falsy" strictly based on the operator chosen.

Ultimately, the choice depends on whether your application treats 0 as a valid state. If you need to preserve zero values, stick with ??. If your logic genuinely requires treating empty strings or zeros as "nothing," then ?: remains a valid tool, provided its behavior is understood and documented.

Our network