What are the potential pitfalls of using variables within square brackets in PHP?

Using variables within square brackets in PHP can lead to unexpected results or errors, especially if the variable is not properly formatted or contains unexpected characters. To avoid this issue, it is recommended to use curly braces {} around the variable within the square brackets to ensure proper variable interpolation.

// Potential pitfalls of using variables within square brackets
$variable = 'key';
$array = ['key' => 'value'];

// Incorrect usage
echo $array[$variable]; // This may lead to unexpected results or errors

// Correct usage
echo $array["{$variable}"]; // Using curly braces ensures proper variable interpolation