TL;DR
-
??(Null Coalescing Operator) Only falls back if the variable is undefined or null.
$page = $_GET['page'] ?? 1;
Good for array keys, request data, and optional variables where values like 0 or "" might still be valid.
-
?:(Elvis Operator) Falls back for any falsy value (false, 0, "", null, etc.).
$per_page = get_option('posts_per_page') ?: 6;
Good for database values or WordPress options where empty or false values should trigger a default.
✅ Simple rule:
Use ?? when you only want to check for missing or null variables.
Use ?: when you want a fallback for anything falsy.
When writing PHP especially in a WordPress environment you’ll often need to provide fallback values when data isn’t available. PHP gives us two operators that look similar but behave very differently:
- The Elvis operator ?:
- The Null Coalescing operator ??
At first glance they seem interchangeable. In practice, they solve two completely different problems, and choosing the wrong one can lead to subtle bugs—particularly when dealing with WordPress functions like get_option().
Let’s break down how they differ and when you should use each one.
The Key Difference
The distinction comes down to what counts as “missing” data.
?? Only null or undefined variables
?: Any falsy value
In PHP, falsy values include:
- null
- false
- 0
- "0"
- "" (empty string)
- Understanding this difference is critical when working with WordPress APIs.
The WordPress get_option() Example
WordPress functions don’t always behave the way you might expect.
For example:
get_option('posts_per_page')
This function can return:
- false → if the option does not exist
- "" (empty string) → if the option exists but has no value
Now consider this code:
$per_page = get_option('posts_per_page') ?? 6;
You might expect it to fall back to 6 when the option is empty.
But it won’t.
The ?? operator only checks for:
- null
- undefined variables
So if get_option() returns:
false
or
""
PHP will treat those as valid values and not use the fallback.
That means $per_page might end up being an empty string instead of 6.
Why the Elvis Operator Works Better Here
Now look at the Elvis operator:
$per_page = get_option('posts_per_page') ?: 6;
This behaves differently.
The Elvis operator checks for falsy values, which includes:
- false
- ""
- null
So if get_option() returns any of those, the fallback 6 is used.
This guarantees that $per_page always contains a usable value.
Does That Mean ?: Is Better Than ???
Not exactly.
They are not competing operators. They solve different problems.
The key is understanding what values you want to allow.
When to Use the Elvis Operator (?:)
Use ?: when you want to reject any falsy value.
Example Use Case
Reading settings from a database where empty or false values are not valid.
$per_page = get_option('posts_per_page') ?: 6;
If the database returns:
- false
- ""
- null
the code will fall back to 6.
This is very common when working with:
- WordPress options
- configuration values
- settings panels
When to Use the Null Coalescing Operator (??)
Use ?? when you only want to handle undefined variables or null values, while preserving valid falsy values.
This is particularly useful for user input or array access.
Example Use Case
Reading query parameters:
$paged = max(1, intval($_GET['epage'] ?? 1));
If $_GET['epage'] does not exist, it falls back to 1.
But if it exists and contains something like:
0
""
PHP will still attempt to parse it instead of replacing it with 1.
This is important in cases where 0 might be a valid value, such as:
- pricing ($price = 0 meaning free)
- counts
- indexes
- pagination offsets
Summary
The Elvis operator and the null coalescing operator look similar, but they solve different problems.
Elvis (?:)
- Rejects any falsy value
- Useful for database values and settings
- Common in WordPress code
Null Coalescing (??)
- Only checks for null or undefined variables
- Preserves valid falsy values like 0
- Ideal for array access and request data
Choosing the right one ensures your fallback logic behaves exactly as intended.
Final Takeaway
If you're working with WordPress options or database-driven values, the Elvis operator often gives you safer behavior:
$per_page = get_option('posts_per_page') ?: 6;
But when handling request data or optional variables, the null coalescing operator is usually the better fit:
$page = $_GET['page'] ?? 1;
Understanding the difference may seem small, but it can prevent subtle bugs that are frustrating to track down later.
And as always in PHP, the right operator depends on what counts as “missing” data in your application.
Top comments (0)