What could be causing the error "Fatal error: Cannot use string offset as an array" in PHP when trying to append keys to a string?

This error occurs when trying to access a string as an array by using square brackets to append keys. To solve this issue, make sure that the variable you are trying to access is actually an array and not a string. You can use functions like `is_array()` to check the type of the variable before trying to access it as an array.

// Example code snippet to fix "Fatal error: Cannot use string offset as an array"
$data = ['key1' => 'value1', 'key2' => 'value2'];

if (is_array($data)) {
    $data['key3'] = 'value3';
    print_r($data);
} else {
    echo "Variable is not an array.";
}