I find myself using array_key_exists() a disturbing amount of times. With a huge number of
if(array_key_exists($key, $array)){
// do stuff with $array[$key];
}
interspersed with
$value = array_key_exists($key, $array)? $array[$key] : "";
Maybe I've developed poor habits by other languages letting you check whether a key exists in an array just by trying to access it.
But the code I'm writing feels super clunky and verbose when array_key_exists() is required most times I'm trying to access an array of data that's not handwritten by me.
Any tips? Maybe I should think about array differently in php? Or are there any modern methods that can help accessing arrays in a more streamlined fashion?
isset()instead? It behaves slightly differently but it's less verbose. Docs here: php.net/manual/en/function.isset.phpisset()returns false if a key exists but is null whereasarray_key_exists()would return true. So if you care about checking for null values,isset()could be more concise in that regard.