PHP ternary operator vs null coalescing operator

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

VS.

$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:

<?php

$a = null;

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

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

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

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

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

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

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

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

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

And it's output:

b
b
a

Notice: Undefined variable: c in /in/apAIb on line 14
a
d
d
e

Notice: Undefined index: c in /in/apAIb on line 33
e

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: https://3v4l.org/McavC

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.

So:

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

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


If you use the shortcut ternary operator like this, it will cause a notice if $_GET['username'] is not set:

$val = $_GET['username'] ?: 'default';

So instead you have to do something like this:

$val = isset($_GET['username']) ? $_GET['username'] : 'default';

The null coalescing operator is equivalent to the above statement, and will return 'default' if $_GET['username'] is not set or is null :

$val = $_GET['username'] ?? 'default';

Note that it does not check truthiness . It checks only if it is set and not null.

You can also do this, and the first defined (set and not null ) value will be returned:

$val = $input1 ?? $input2 ?? $input3 ?? 'default';

Now that is a proper coalescing operator.


Both of them behave differently when it comes to dynamic data handling.

If the variable is empty ( '' ) the null coalescing will treat the variable as true but the shorthand ternary operator won't. And that's something to have in mind.

$a = NULL;
$c = '';

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

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

print $c ?? '1d';
print "n";

print $c ?: '2d';
print "n";

print $e ?? '1f';
print "n";

print $e ?: '2f';

And the output:

1b
2b

2d
1f

Notice: Undefined variable: e in /in/ZBAa1 on line 21
2f

Link: https://3v4l.org/ZBAa1

链接地址: http://www.djcxy.com/p/9940.html

上一篇: 什么是线程安全或非线程安全?

下一篇: PHP三元运算符vs null合并运算符