What are the potential pitfalls of using square brackets to access array values in PHP?

Using square brackets to access array values in PHP can lead to errors if the array key does not exist. To avoid potential pitfalls, you can check if the key exists in the array before accessing it using isset() or array_key_exists() functions. This will prevent PHP from throwing undefined index notices or warnings.

$array = ['key1' => 'value1', 'key2' => 'value2'];

// Check if the key exists before accessing it
if(isset($array['key3'])) {
    $value = $array['key3'];
    echo $value;
} else {
    echo "Key does not exist in the array.";
}