Asked  7 Months ago    Answers:  4   Viewed   43 times

Can someone explain the differences between ternary operator shorthand (?:) and null coalescing operator (??) in PHP?

When do they behave differently and when in the same way (if that even happens)?

$a ?: $b


$a ?? $b



When your first argument is null, they're basically the same except that the null coalescing won't output an E_NOTICE when you have an undefined variable. The PHP 7.0 migration docs has this to say:

The null coalescing operator (??) has been added as syntactic sugar for the common case of needing to use a ternary in conjunction with isset(). It returns its first operand if it exists and is not NULL; otherwise it returns its second operand.

Here's some example code to demonstrate this:


$a = null;

print $a ?? 'b'; // b
print "n";

print $a ?: 'b'; // b
print "n";

print $c ?? 'a'; // a
print "n";

print $c ?: 'a'; // Notice: Undefined variable: c in /in/apAIb on line 14
print "n";

$b = array('a' => null);

print $b['a'] ?? 'd'; // d
print "n";

print $b['a'] ?: 'd'; // d
print "n";

print $b['c'] ?? 'e'; // e
print "n";

print $b['c'] ?: 'e'; // Notice: Undefined index: c in /in/apAIb on line 33
print "n";

The lines that have the notice are the ones where I'm using the shorthand ternary operator as opposed to the null coalescing operator. However, even with the notice, PHP will give the same response back.

Execute the code:

Of course, this is always assuming the first argument is null. Once it's no longer null, then you end up with differences in that the ?? operator would always return the first argument while the ?: shorthand would only if the first argument was truthy, and that relies on how PHP would type-cast things to a boolean.


$a = false ?? 'f'; // false
$b = false ?: 'g'; // 'g'

would then have $a be equal to false and $b equal to 'g'.

Wednesday, March 31, 2021
answered 7 Months ago

I'm tempted to lie and say that English is my second language...but the truth is that I just have no idea what 'Coalescing' means. I know what ?? 'does' in C#, but the name doesn't make sense to me.

I looked up the word and I understand it to be a synonym for 'join'.

I'd say a more accurate description of "coalesce" would be "to form one thing from different elements". The "coalescing" of the ?? operator happens because a single value is always resolved from one of the two values. The first non-null value is the result.

Friday, June 25, 2021
answered 4 Months ago

Answer to your first question: Yes.

Short answer to your second question: None, you should choose based on which is more readable.

Answer to your third question: if you expect the whole expression to run "in one shot" and therefore not be subject to concurrency issues, then no, the null-coalescing operator does not guarantee that as explained in the answer of this Stackoverflow question. In both your examples you would actually face the same concurrency challenges.

Long answer to your second question:

Looking in the Microsoft '??' doc, all is mentioned is the operator purpose and function:

The ?? operator is called the null-coalescing operator. It returns the left-hand operand if the operand is not null; otherwise it returns the right hand operand.

Hence, the null-coalescing operator makes you write cleaner code that would otherwise require you to write the operand in question twice (as in your 2nd example).

Usage of the null-coalescing operator is more related to utility than performance, as explained is the accepted answer of a similar Stackoverflow question. Indeed, both perform quite the same.

Interesting to notice, as part of the same answer, the null-coalescing operator seems to perform slightly faster, but the difference is so little that could be ignored.

Wednesday, September 1, 2021
answered 2 Months ago

This happens as consequence of the assignment operator also returning the value:

The assignment operator (=) stores the value of its right-hand operand in the storage location, property, or indexer denoted by its left-hand operand and returns the value as its result.

The expression b = 12 not only assigns 12 to b, but also returns this value.

Thursday, September 23, 2021
Michael Yuwono
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :