What are the best practices for accessing arrays in PHP functions?

When accessing arrays in PHP functions, it is important to ensure that the array is properly checked for existence before trying to access its elements. This can help prevent errors and notices in your code. One way to do this is by using the isset() function to check if the array key exists before accessing it.

function accessArrayElement($array, $key) {
    if(isset($array[$key])) {
        return $array[$key];
    } else {
        return null;
    }
}

// Example usage
$array = ['foo' => 'bar', 'baz' => 'qux'];
$element = accessArrayElement($array, 'baz');
echo $element; // Output: qux