0

Considering the following code:

if (isset($array['key']) && $array['key'] == 'some value') {
    // do something...
} else {
    // do something else...
}

Is there a way I can simplify the code so that I don't have to check with isset every time I want to use an array value in an if clause? Something like $array['key'] ?= 'some value'

DarkBee
  • 16,592
  • 6
  • 46
  • 58
Xathon
  • 15
  • 5
  • 3
    How about the [null coalescing operator](https://www.php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op)? `if (($array['key'] ?? '') == 'some value')` – billyonecan Sep 07 '22 at 09:46
  • @billyonecan I never know that it can be use like that `if (($var ?? '') == 'check')`. Thank you. :) – vee Sep 07 '22 at 09:58

0 Answers0