What are the potential pitfalls of using constants as array indexes in PHP?

Using constants as array indexes in PHP can lead to potential pitfalls because constants are evaluated at compile time, not runtime. This means that if the constant value changes after the array is defined, the array will not be updated accordingly. To solve this issue, you can use a function to dynamically generate the array key based on the constant value at runtime.

<?php
define('KEY', 'my_key');

function getKey() {
    return KEY;
}

$array = [
    getKey() => 'value'
];

print_r($array);
?>