PHP ternary operator vs null coalescing operator

balping

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
MasterOdin

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'; // 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: 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'; // false
$b = false ?: 'g'; // 'g'

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

PHP Null coalescing operator usage

From Java

What is null coalescing assignment ??= operator in PHP 7.4

From Java

Is there a "null coalescing" operator in JavaScript?

From Dev

Null coalescing operator override

From Dev

Null coalescing operator with bool

From Dev

Null coalescing operator and casting with as

From Dev

Null coalescing operator override

From Dev

?? (null coalescing) vs ? (ternary if) expression

From Dev

Null Coalescing Operator in F#?

From Dev

null coalescing operator assignment to self

From Dev

null-coalescing operator in the getter

From Dev

Usage of ?? operator (null-coalescing operator)

From Dev

Usage of ?? operator (null-coalescing operator)

From Dev

understading the ternary operator in php

From Dev

evaluation of ternary operator in php

From Dev

Ternary Operator in PHP

From Dev

PHP Ternary Operator Misunderstanding?

From Dev

evaluation of ternary operator in php

From Dev

sql null in c# and null coalescing operator

From Dev

null-coalescing operator on a property get

From Dev

Implicit conversion with null-coalescing operator

From Dev

Is there an equal for the c# null coalescing operator in java?

From Dev

null-coalescing operator with wildcard in a lambda expression

From Dev

Is there a way to implement and make use of a "NOT null coalescing" operator?

From Dev

Is there an equivalent of isset or a null coalescing operator in SASS?

From Dev

Is there a way to implement and make use of a "NOT null coalescing" operator?

From Dev

Is there an equivalent of isset or a null coalescing operator in SASS?

From Dev

How to define a null-coalescing operator for Twig?

From Dev

Performance of expression behind null coalescing operator

Related Related

HotTag

Archive