PHP Elvis Operator: Complete Guide with Examples

The Elvis operator in PHP is a shorthand way to return a value when another is not set or is null. It helps write shorter and cleaner code when checking for null or undefined variables.

In this guide, you’ll learn what the Elvis operator is, how it works in PHP, and when to use it. We’ll also compare it to other similar operators and provide real-world examples.

What Is the Elvis Operator in PHP?

The Elvis operator is not an official PHP operator, but a nickname given to the null coalescing operator ?? because it resembles a sideways Elvis emoticon: ?:.

In PHP, the Elvis operator is used to return the first operand if it's set and not null. If it is null, it returns the second operand.

$value = $a ?? $b;

This line means:

  • If $a exists and is not null, assign it to $value.

  • Otherwise, use $b.

Syntax

$variable = $possiblyNullValue ?? $defaultValue;

You can also chain it:

$username = $input['username'] ?? $_POST['username'] ?? 'guest';

Here:

  • If $input['username'] is set and not null, it’s used.

  • If not, check $_POST['username'].

  • If both are null, use 'guest'.

How It Works

The Elvis operator uses short-circuit evaluation:

  • PHP checks operands from left to right.

  • It returns the first non-null value.

  • If all values are null, it returns the final one.

Common Use Cases

1. Setting Defaults

$name = $_GET['name'] ?? 'Guest';

If the user didn't pass name in the query string, the value defaults to 'Guest'.

2. Fallback Logic

$color = $userSettings['color'] ?? $siteSettings['color'] ?? 'blue';

It checks for user preference, then site default, then uses 'blue'.

3. Cleaner Ternary Statements

Before:

$title = isset($pageTitle) ? $pageTitle : 'Untitled';

With Elvis:

$title = $pageTitle ?? 'Untitled';

Elvis Operator vs Ternary Operator

Here is a table that shows the PHP Elvis operator vs the null coalescing operator.

OperatorSyntaxUse When
Null coalescing (??)$a ?? $bWhen checking for null or undefined variables
Ternary (?:)isset($a) ? $a : $bWhen you need more control or evaluate expressions

Note: ?? only checks for null, not false or empty.

$val = null ?? 'default'; // returns 'default'
$val = false ?? 'default'; // returns false

Elvis Operator vs Null Coalescing Assignment (??=)

PHP 7.4+ introduced the null coalescing assignment operator:

$user['name'] ??= 'Anonymous';

This means:

  • If $user['name'] is not set or is null, assign 'Anonymous'.

It’s the same as:

if (!isset($user['name'])) {
    $user['name'] = 'Anonymous';
}

PHP Version Support

  • ?? Elvis operator: PHP 7.0+

  • ??= assignment operator: PHP 7.4+

If you're using PHP 5.x or older, you must use isset() manually or upgrade your PHP version.

Common Mistakes

1. Using Elvis with functions that return false

$val = strpos('abc', 'x') ?? 'Not found';

This returns false if not found — but not null. So Elvis doesn’t kick in. Use strict comparison:

$val = (strpos('abc', 'x') !== false) ? 'Found' : 'Not found';

2. Confusing with ternary operator

$a = $value ?: 'default'; // returns default if value is falsy (false, 0, '', etc.)
$b = $value ?? 'default'; // returns default only if value is null

Use ?? for null checks. Use ?: if you want to catch false or empty values too.

Real-World Example: User Input

function getUsername(array $input) {
    return $input['username'] ?? 'Guest';
}

echo getUsername($_GET);

If the URL has ?username=John, it prints John. Otherwise, it prints Guest.

Summary

The Elvis operator (??) in PHP makes your code cleaner and easier to read. It helps you handle default values without long if statements or isset() checks.

Key Takeaways:

  • Use ?? to return the first non-null value.

  • It’s safe to use even if the variable is not defined.

  • It’s faster and shorter than using isset() or ternary operators.

  • Use ??= to set defaults directly into variables or arrays.

FAQ

Is the Elvis operator official in PHP?

No, it’s just a nickname for the null coalescing operator ??.

Can I use Elvis for false or 0?

No. ?? checks only for null, not falsy values like 0, false, or ''.

What’s the difference between ?? and ?:?

  • ?? checks for null.

  • ?: checks for falsy (null, false, '', 0).

Final Tip

Use the Elvis operator whenever you need a quick fallback for possibly null values. It keeps your code short, safe, and readable.

Follow Flatcoding tutorials to learn PHP.

0
Subscribe to my newsletter

Read articles from Patricia Hernandez directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Patricia Hernandez
Patricia Hernandez