The Meaning of question mark colon operator?

I was diving into Symfony framework 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?

It’s just a shorthand version of a if statement or ternary operator (that you show). As for isset and null, just think of isset as the name implies and null is there is nothing there. Doing $env = null; is the same thing ans $env = “”; In this case I would use isset.

Here’s a good explanation of it including nested tenary’s. ( They can get hard to read though so I use just IF-ELSE instead… Tenary’s

You’re correct with your second guess: two questions marks together is the null coalescing operator, new in PHP 7.

Sponsor our Newsletter | Privacy Policy | Terms of Service