What does double question mark (??) operator mean in PHP

elkolotfi
:

I was diving into Symfony framework (version 4) code and found this piece of code:

$env = $_SERVER['APP_ENV'] ?? 'dev';

I'm not sure what this actually does but I imagine that it expands to something like:

$env = $_SERVER['APP_ENV'] != null ? $_SERVER['APP_ENV'] : 'dev';

Or maybe:

$env = isset($_SERVER['APP_ENV']) ? $_SERVER['APP_ENV'] : 'dev';

Does someone have any precision about the subject?

EDIT:

To all the people who marked my question as negative because there's already a similar question (PHP ternary operator vs null coalescing operator):

It is true that both questions are very similar. However it is hard for everybody to imagine that the "??" is called the coalescing operator.

Otherwise I could easy find it on the official documentation:

http://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op

However, for someone who didn't know that this feature was added in php 7 it's more likely to type:

"php ?? operator" or "php double question mark operator"

And here is why my question has an added value.

michalhosna
:

It's the "null coalescing operator", added in php 7.0. The definition of how it works is:

It returns its first operand if it exists and is not NULL; otherwise it returns its second operand.

So it's actually just isset() in a handy operator.

Those two are equivalent1:

$foo = $bar ?? 'something';
$foo = isset($bar) ? $bar : 'something';

Documentation: http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.coalesce

In the list of new PHP7 features: http://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op

And original RFC https://wiki.php.net/rfc/isset_ternary


EDIT: As this answer gets a lot of views, little clarification:

1There is a difference: In case of ??, the first expression is evaluated only once, as opposed to ? :, where the expression is first evaluated in the condition section, then the second time in the "answer" section.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What does hypothesis with operator with question mark mean

From Dev

What does .php question mark = mean?

From Java

What does question mark and dot operator ?. mean in C# 6.0?

From Dev

What does question mark mean?

From Dev

What does question mark equals mean in CoffeeScript?

From Dev

What does this question mark mean in Flow: "?() => void"

From Dev

What does the question mark in "Decimal?" mean?

From Dev

XML what does that question mark mean

From Dev

What does the triple question mark mean in scala?

From Dev

What does the question mark in java mean?

From Dev

What does the question mark in terminal command mean?

From Dev

What does the "?" (question mark) mean in javascript?

From Dev

What does this two question mark mean?

From Dev

What does the question mark in terminal command mean?

From Dev

What does PHP operator ->{...} mean?

From Dev

What does "type" mean and is there is a special use for a question mark in ECMA 6?

From Dev

What does the question mark in member access mean in C#?

From Dev

What does the question mark mean in a type parameter bound?

From Dev

What does the question mark icon by a file name mean in Aptana?

From Java

What is this question mark operator about?

From Dev

Vim: What does a double quotation mark mean at the beginning of a command?

From Dev

What does this PHP syntax mean? (question marks)

From Dev

What does %-mark mean in Clojure?

From Dev

What do a question mark (?) and an ampersand (&) mean in a URL?

From Dev

What does operator >>= mean

From Dev

What does the <> operator mean

From Dev

What does this operator mean? /=

From Dev

What does operator ':=' mean?

From Dev

What does the operator ?= mean?

Related Related

HotTag

Archive