What are common causes of the "Illegal string offset" error in PHP?

The "Illegal string offset" error in PHP occurs when trying to access an index of a string as if it were an array. This error typically happens when trying to access a specific character of a string using square brackets with an index. To solve this issue, make sure that you are treating the variable as a string and not an array.

// Incorrect usage causing "Illegal string offset" error
$string = "Hello";
echo $string[0]; // Trying to access the first character of the string as an array

// Correct usage
$string = "Hello";
echo $string; // Output: Hello