1

How can I create a shorter expression of:

$variable = @$array["property"] ? $array["property"] : DEFAULT_VALUE_CONSTANT;

To something like this:

$variable = @$array["property"] || DEFAULT_VALUE_CONSTANT;

Now I get true / false

1 Answers1

10

Yes it's possible in PHP7 with Null coalescing operator (??)

$variable = $array["property"] ?? DEFAULT_VALUE_CONSTANT;

If you are using PHP version < 7 one solution is use the elvis operator

$variable = $array["property"] ?: DEFAULT_VALUE_CONSTANT;

Please avoid using @ instead of isset().

References:

?: operator (the 'Elvis operator') in PHP

Community
  • 1
  • 1
rray
  • 2,518
  • 1
  • 28
  • 38