php - Using ternary operator in echo?

Solution:

The ?? Operator in PHP is the Null Coalescing Operator:

expr1 ?? expr2;

expr1 is returned when expr1 exists and is NOT null; otherwise it returns expr2.

Since in this case expr1 is false but is set, this expression returns Boolean false.

Compare:

echo false ?? 'It is FALSE'; // won't be displayed
echo null ?? 'it is NULL'; // It will work

Echo does not output when passed Boolean false.

Answer

Solution:

as said, you're using the null coalescing operator here

It's checking if false is null

just want to add: regular vs. shorthand ternary operation

echo false ? 'yes' : 'no';

is the same as

echo false ?: 'no';

So,

echo true ?: 'no';

will output 1, as ?: essentially skips the first expression

(condition) ? expression1 : expression2

Source