Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 890
elkolotfi Avatar asked Dec 04 '18 10:12

elkolotfi


People also ask

What does double question mark do in PHP?

In PHP 7, the double question mark(??) operator known as Null Coalescing Operator. It returns its first operand if it exists and is not NULL; otherwise, it returns its second operand. It evaluates from left to right.

What is the double question mark operator?

In simplest way, two question marks are called "Coalescing Operator", which returns first non null value from the chain. e.g if you are getting a values from a nullable object, in a variable which is not nullable, then you can use this operator.

What does ?: Mean in PHP?

The Scope Resolution Operator (also called Paamayim Nekudotayim) or in simpler terms, the double colon, is a token that allows access to static, constant, and overridden properties or methods of a class.

What does double question mark mean in react?

Double question marks(??) or nullish coalescing operator helps us to assign default values to null or undefined variables in Angular and Typescript. It's often called as Null coalescing operator.


2 Answers

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.

like image 78
michalhosna Avatar answered Oct 11 '22 14:10

michalhosna


$myVar = $someVar ?? 42; 

Is equivalent to :

$myVar = isset($someVar) ? $someVar : 42; 

For constants, the behaviour is the same when using a constant that already exists :

define("FOO", "bar"); define("BAR", null);  $MyVar = FOO ?? "42"; $MyVar2 = BAR ?? "42";  echo $MyVar . PHP_EOL;  // bar echo $MyVar2 . PHP_EOL; // 42 

However, for constants that don't exist, this is different :

$MyVar3 = IDONTEXIST ?? "42"; // Raises a warning echo $MyVar3 . PHP_EOL;       // IDONTEXIST 

Warning: Use of undefined constant IDONTEXIST - assumed 'IDONTEXIST' (this will throw an Error in a future version of PHP)

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 :

$myVar = @constant("IDONTEXIST") ?? "42"; // No warning displayed anymore echo $myVar . PHP_EOL; // 42 
like image 21
Cid Avatar answered Oct 11 '22 15:10

Cid