The following code understandably yields "dev" when $_SERVER['MODE'] is not set:
$_SERVER['MODE'] ?? null === 'production' ? 'prod' : 'dev'
That is because $_SERVER['MODE'] ?? null resolves to NULL which, of course, is not strictly equal to "production", so the right operand of the ternary expression prevails.
However, if I set $_SERVER['MODE'] to just any truthy value, things become strange. The code yields "prod"!
I do not understand why. When $_SERVER['MODE'] is set, the null coalescing resolves to whatever value it is set to. So, unless it is set to "production", the right operand of the ternary expression should still prevail. Why does it not?
Besides, if I add brackets like this:
($_SERVER['MODE'] ?? null) === 'production' ? 'prod' : 'dev'
— then it works as expected. But why not without the brackets?
Related but not dupe: PHP null coalesce + ternary operators strange behavior
P.S. This confusion just costed me $40. $_SERVER['MODE'] was set to "dev" but, as the expression resolved to "prod", real AWS instances were started :D.
( 3 + 3 ) * 3 = 18vs3 + 3 * 3 = 12