I was diving into Symfony framework (version 4) code and found this peace of code:
$env = $_SERVER['APP_ENV'] ?? 'dev';
I'm not pretty 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';
Someone has any precision about the subject?
EDIT:
To all the people who answered the question: thank you 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.
I ask you to, please, reconsider your negative feedback. Thanks
Regards, Epixilog
Is equivalent to :
For constants, the behaviour is the same when using a constant that already exists :
However, for constants that don't exist, this is different :
Php will convert the non-existing constant to a string.
You can use
constant("ConstantName")
that returns the value of the constant or null if the constant doesn't exist, but it will still raise a warning. You can prepended the function with the error control operator@
to ignore the warning message :It's the "null coalescing operator", added in php 7.0. The definition of how it works is:
So it's actually just
isset()
in a handy operator.Those two are equivalent1:
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.is short hand for x = y if y is set, otherwise x = 'dev'
There is also
meaning if y equals 'SOMETHING' then x = 10, otherwise x = 20