I frequently use the elvis operator to set default values. Eg I have an array $x = ['value' => $value];
, and then in another section of my code, $val = $x['value'] ?: '22';
In the above scenario, if $value
is 3, $val should end up as 3 as well, but if $value
is undefined or 0 or something, it should be 22.
When I run this sort of code on php playground, even if $x itself hasn't been defined, or if $x
has no 'value' property, or if $value
is not defined, it outputs 22.
However, when I run this same code on my Laravel server, the behavior is different. If $x
is defined, and $x['value']
is defined as something falsy, then the evlis operator works as expected. But if $x
isn't defined, or $x['value']
hasn't been set, I get an error. For example if $x
is defined but $x['value']
isn't, I get an 'Undefined index' error. Here are some concrete examples, and what happens in both environments
$x = ['value' => 0];
$val = $x['value'] ?: '22';
// this works on both my laravel server and the playground appropriately
$x = [];
$val = $x['value'] ?: '22';
// on Laravel, this errors, but on the playground it sets val to 22
unset($x);
$val = $x['value'] ?: '22';
// on Laravel, this errors, but on the playground it sets val to 22
So I'm just a bit confused about the elvis operator and how it's expected to deal with undefined values and unset indexes. Most of the time, I'd actually prefer it to behave in the way the playground is behaving -- treating undefined values as 'falsy' and defaulting to the right side of the operator. But I'm not sure what the expected behavior really is.