Null coalescing (??) operator vs Elvis (?:) operator in PHP – they are different!

A common mistake that some developers may incur into is using the ?? and ?: as equivalents in languages that support them both like modern PHP. There is a very simple yet significant difference between them.

Basically, the Elvis operator evaluates the truth of the first term; whereas the null coalescing evaluates if it is not null.

We can see very easily why they are different and should not be mistaken with the help of the PHP interactive shell:

php > var_dump( false ?: 'string' );
string(7) "vestido"

php > var_dump( false ?? 'vestido' );
bool(false)

The Elvis operator is just a ternary operator that leaves out the second operand (the return value if the condition evaluates to true) from the declaration, actually using the same evaluated expression. That means that if the first operand is true, it will return it. Otherwise it will return the second one. Let’s see a quick example:

<?php
$falseCondition = 0;
$trueCondition = 1;
$nullCondition = null;

echo $falseCondition ?: 'second'; //will output 'second'
//is the same as..
echo $falseCondition ? $falseCondition : 'second';
//which is the same as
if ( $falseCondition ) {
    echo $falseCondition;
} else {
    echo 'second';
}

echo $trueCondition ?: 'second'; //will output '1'
echo $nullCondition ?: 'second'; //will output 'second'

On the other hand, the null coalescing operator evaluates the if the first operand exists and is not null. If so, it returns it; otherwise it returns its second operand.

<?php
$falseCondition = 0;
$trueCondition = 1;
$nullCondition = null;

echo $falseCondition ??  'second'; //will output '0'
//is the same as..
echo isset( $falseCondition ) ? $falseCondition : 'second';
//which is the same as
if ( isset( $falseCondition ) ) {
    echo $falseCondition;
} else {
    echo 'second';
}

echo $trueCondition ?? 'second'; //will output '1'
echo $nullCondition ?? 'second'; //will output 'second'

Leave a Reply