I have this if statement
if ($something !== $array[$key]) {
// do something
}
But I can't be 100% sure that $array[$key]
exists.
So I'd like to do something like this:
if ($something !== $array[$key] ?? null) {
// do something
}
The if statement should run when the array key does not exist or or different from the variable. Of course, I could do something like this:
if ($something !== (isset($array[$key]) ? $array[$key] : null)) {
// do something
}
But I want to avoid this since it makes the code less readable. Are there any other options except this one:
$compare = $array[$key] ?? null;
if ($something !== $compare) {
// do something
}
Following code might help:
if (!array_key_exists($key, $array) || $array[$key] !== $something) {
// Do it
}